Advertise Here

Author Topic: Need help logging in  (Read 6573 times)

0 Members and 1 Guest are viewing this topic.

guest4485

  • Guest
Need help logging in
« on: February 28, 2009, 01:20:58 am »
Hi, I am new to PHP and MySQL but I decided that I would take on the challenge of creating a game and learn as I go. I have a nice layout and information for my pages, but when I try to login it displays the error "Please enter a username" and I think I have it set correctly to display that if the username field is left blank, but it displays even if I enter my username and password.

Here is the php code for index.php(equivalent to login.php).

Code: [Select]
<?php
session_start
();
EOF;
$ip = ($_SERVER['HTTP_X_FORWARDED_FOR'])
    ?  
$_SERVER['HTTP_X_FORWARDED_FOR']
    :  
$_SERVER['REMOTE_ADDR'];
if(
file_exists('ipbans/'.$ip))
{
die(
"<b><font color=red size=+1>Your IP has been banned, there is no way around this.</font></b></body></html>");
}
$dbhost "mysql3.freehostia.com";
$dbname "jasjon63_acacian";
$dbuser "jasjon63_acacian";
$dbpass "279656";
$username mysql_real_escape_string($_POST['username']);
$password mysql_real_escape_string($_POST['password']);
mysql_connect $dbhost$dbuser$dbpass)or die("Could not connect: ".mysql_error());
mysql_select_db($dbname) or die(mysql_error());
$dbhost "mysql3.freehostia.com";
$dbname "jasjon63_acacian";
$dbuser "jasjon63_acacian";
$dbpass "279656";
$username mysql_real_escape_string($_POST['username']);
$password mysql_real_escape_string($_POST['password']);
mysql_connect $dbhost$dbuser$dbpass)or die("Could not connect: ".mysql_error());
mysql_select_db($dbname) or die(mysql_error());
?>


and this is the form for the index.php page
Code: [Select]
<form name="login" method="post" action="authenticate.php">
  <table width="100%" border="0" align="center" cellpadding="2" cellspacing="0">
    <tr>
      <td width="50%"><b>Login</b></td>
      <td width="50%"><input name="login" type="text" class="textfield" id="login" /></td>
    </tr>
    <tr>
      <td width="50%"><b>Password</b></td>
      <td width="50%"><input name="password" type="password" class="textfield" id="password" /></td>
    </tr>
    <tr>
      <td width="50%">&nbsp;</td>
      <td width="50%"><input type="submit" name="Submit" value="Login" /></td>
    </tr>
  </table>
</form>
and this is the code for the authenticate.php

Code: [Select]
<?php

$dbhost 
"mysql3.freehostia.com";
$dbname "jasjon63_acacian";
$dbuser "jasjon63_acacian";
$dbpass "279656";
$username mysql_real_escape_string($_POST['username']);
$password mysql_real_escape_string($_POST['password']);
mysql_connect $dbhost$dbuser$dbpass)or die("Could not connect: ".mysql_error());
mysql_select_db($dbname) or die(mysql_error());

if(empty(
$_POST['username']))
{
die("Please enter a username.");
}
$query "SELECT `username`,`password` FROM user WHERE `username`='".$username."'";
$username."'";
$result mysql_query($query);
if(
mysql_num_rows($result)!=1)
{
die('We don\'t have a user called '.$username.'. If this is your first visit to our website, you may need to <a href="../register.php">create an account</a>. Otherwise, check your spelling.');
}
while(
$record mysql_fetch_assoc($result))
{
if(md5($password)!=$record['password'])
{
die("You provided an incorrect password. Please try again.");
}
}

?>


Thanks in advanced for any help given,
Jason.

Offline Colette Brunel

  • SMF For Free Sr. Member
  • *
  • Posts: 424
    • View Profile

  • Total Badges: 17
    Badges: (View All)
    Poll Voter Level 4 Fourth year Anniversary Windows User Topic Starter
Re: Need help logging in
« Reply #1 on: March 24, 2009, 03:47:19 pm »
That's rather dirty coding. Here, I revised it for you.

Code: [Select]
* The PHP syntax BELOW should be in a file named: index.php

<?php
# Created by ccbtimewiz (ccbtimewiz@jeunosky.net)
# ====================================================== #

// Defining all things called by this file as 'Cornet'
define('Cornet'1);

// Grabbing the server PHP file...
require_once(dirname(__FILE__) . '/Server.php');

// Ending the session.
session_destroy();

// End of everything.
exit();
?>





* The PHP syntax BELOW should be in a file named: Server.php

<?php
# Created by ccbtimewiz (ccbtimewiz@jeunosky.net)
# ====================================================== #

if (!defined('Cornet'))
die();

function 
clean_username($user)
{
// An array of special characters that we don't want...
$bad_characters array_merge(range('{''~'), range(':''@'));

// We're cleaning the username by removing ALL special characters.
if (!empty($user))
str_replace($bad_characters''$user);

return $user;
}

// This is faster and more secure-- creating a file deep within root that stores all banned IPs.
$banned_ips file_get_contents('home/user/.bannedips');

// Were we able to get the banned ips file? If not, we're killing this script.
if (empty($banned_ips))
exit();

// Creating and assigning a name to a session.
session_start();

// Grabbing the IP address using getenv(), which works similar to the super-global $_SERVER, but faster.
$user_ip getenv('HTTP_X_FORWARDED_FOR') ? getenv('HTTP_X_FORWARDED_FOR') : getenv('REMOTE_ADDR');

// If their IP exists in the file.. that means they're banned! >:)
if (!strpos($banned_ips$user_ip) !== false)
die("<strong>You are banned from accessing this form!</strong>");

// Server setting information...
$dbhost 'mysql3.freehostia.com';
$dbname 'jasjon63_acacian';
$dbuser 'jasjon63_acacian';
$dbpass '279656';
// ---------------------------------------------

// Connecting to the database using the server setting information.
$connection mysql_pconnect($dbhost$dbuser$dbpass);

// If we weren't able to connect, stop the script and show an error.
if (!$connection)
die('Unable to query database. Error returned:/n<tt>' mysql_error() . '</tt>');

// Selecting which database to use...
$db_selected mysql_select_db($dbname$connection);

// If we weren't able to select this database, stop the script and show an error.
if (!$db_selected)
die('Unable to select ' $dbname ' as a database user. Error returned: /n<tt>' mysql_error() . '</tt>');

// Escaping the information that was retrivied from the login form.
$username mysql_real_escape_string(clean_username($_REQUEST['username']));
$password mysql_real_escape_string($_REQUEST['password']);

// We're logging them in. :)
require_once('Login.php');

?>





* The PHP syntax BELOW should be in a file named: Login.php

<?php
# Created by ccbtimewiz (ccbtimewiz@jeunosky.net)
# ====================================================== #

if (!defined('Cornet'))
die();

function 
Login()
{
$is_logged false;
global $username$password;

// Did they forget to submit their username?
if (empty($_REQUEST['username']))
die('You forgot to fill in a username.');

// Did they forget to submit their password?
if (empty($_REQUEST['passwd']))
die('You forgot to fill in a password.');

// Querying the DB with the submitted information, so we can check and/or verify. Then eventually log them in.
// !!! I added a new field to keep track of users, named 'id'
$query "
SELECT `username`, `password`, 'id'
FROM user
WHERE `username`='" 
$username "'
"
;

// Storing the query into query format in $result.
$result mysql_query($query);

// If the record was not found, then the username doesn't exist. Show an error.
if (mysql_num_rows($result) != 1)
die("The username you submitted ($username) was not valid. If this is your first visit to this website, you may need to <a href=\"../register.php\">create an account</a>.");

// Checking out the result...
while ($row mysql_fetch_assoc($result))
{
// If their password is wrong, stop and tell them this. Else, continue with the information.
if (sha1($password) !== $row['password'])
die('The password you submitted was incorrect.');
else
$is_logged true;

// If the user got their pass right, let's store their information into an array.
if ($is_logged)
{
$user = array(
'id' => $row['id'],
'email' => $row['email'],
'username' => $row['username'],
'password' => $row['password'],
);

// And then echo all the information for the hell of it.
echo "
Welcome, 
{$user['username']}. You're currently logged in.\n
Your password is 
{$user['password']}, and your email is {$user['email']}.\n
Your user ID is 
{$user['id']}.\n
"
;
}
}
mysql_free_result($result);
}

// Calculate the login.
login();

// Set the cookie
setcookie("LoginData"$usernametime()+3600);

?>




* The (x)HTML syntax below should be in a file named: Template-Login.php


<html>
<head>
<title>Login</title>
</head>
<body>
<form name="login" method="post" action="authenticate.php">
<table width="100%" border="0" align="center" cellpadding="2" cellspacing="0">
<tr>
<td width="50%"><b>Login</b></td>
<td width="50%"><input name="login" type="text" class="textfield" id="login" /></td>
</tr>
<tr>
<td width="50%"><b>Password</b></td>
<td width="50%"><input name="password" type="password" class="textfield" id="password" /></td>
</tr>
<tr>
<td width="50%">&nbsp;</td>
<td width="50%"><input type="submit" name="Submit" value="Login" /></td>
</tr>
</table>
</form>
</body>
</html>

Offline black18

  • SMF For Free Newbie
  • *
  • Posts: 1
    • View Profile

  • Total Badges: 5
    Badges: (View All)
    Topic Starter Level 1 First Post Second year Anniversary One year Anniversary
Re: Need help logging in
« Reply #2 on: June 01, 2009, 09:44:50 pm »
I think it was great challenge to  you that you have a new layout from the PHP and MySQL.



_________________
Scotsman Ice Machine




 

Related Topics

  Subject / Started by Replies Last post
14 Replies
4055 Views
Last post June 30, 2006, 10:12:50 am
by Crasy
6 Replies
2110 Views
Last post May 06, 2007, 02:19:42 pm
by Crasy
11 Replies
2375 Views
Last post January 17, 2008, 08:57:43 pm
by KEA
2 Replies
1263 Views
Last post May 22, 2008, 02:13:14 am
by Kiwigold
6 Replies
1613 Views
Last post September 22, 2008, 10:23:01 am
by Henry™