Jump to content

Whats wrong with my code?? Please help


AbydosGater

Recommended Posts

Hi, Ive been trying to update my login script to work on my friends website, only better....

The code is as following..
[code]
<?php
if (!$_SESSION['username'] && !$_POST['login']){ // No session + No Form Login... Display the form...
echo <<<LOGINFORM
<form action="test.php" method="post" name="loginform">
Username:
<br>
<input type="text" name="username" value="" size="20" class="field1">
<br>
Password:
<br>
<input type="password" name="password" value="" size="20" class="field1">
<br><br>
<input type="submit" value="login" class="button1">
<br><br>
<a href="register.php">Click Here To Register</a>
</form>
LOGINFORM;
} elseif ($_POST['username']){ //if the form has been submitted... The ifs + elses between this and next comment arnt that important just checking if the login details are correct...

$username = $_POST['username'];
$password = $_POST['password'];
//---
$result = mysql_query("SELECT * FROM sf_users WHERE username='$username'") or die(mysql_error());
$user = mysql_fetch_array( $result );
if ($user['member_id'] == ""){
echo "<font color=\"#FF0000\"><b>Unknown username, please try again</b></font>";
} else if ($user['member_id'] !== ""){
$dbusername = $user['username'];
$dbpassword = $user['password'];
if ($username == $dbusername && $password == $dbpassword){
session_register("user");
$_SESSION['username'] = $username;
$_SESSION['password'] = $password;
//-- Rest Of Info On The User
$_SESSION['member_id'] =  $user['member_id'];
$_SESSION['email'] =  $user['email'];
$_SESSION['CL'] =  $user['CL'];
$_SESSION['banned'] =  $user['banned'];
//-- Session Variables End
echo "<font color=\"#FF0000\"><b>Thank you for login in,<br /> You will be redirected to the protected pages in 2 seconds <META HTTP-EQUIV=\"refresh\" CONTENT=\"2; URL=index.php\"></b></font>";
} else if ($username == $dbusername && $password !== $dbpassword){
echo "<font color=\"#FF0000\"><b>Your Password is incorrect</b></font>";

} else {
echo "<font color=\"#FF0000\"><b>Unknown System Error!<br> Please ensure your Cases are correct!</b></font>";
}
};


} elseif ($_SESSION['username']){ //If we have the session... echo the username has logged in :P
//Display member options...
$username = $_SESSION['username'];
echo "<font color='#FFFFFF'><b>$username.. You are logged in.</b></font>";
} else { //WOOPS somethings gone wrong if i see this...
echo "<font color='#FFFFFF'><b>Unknown System Error.</b></font>";
}

?>
[/code]

Its not working, when the form is submitted, it just displays the login for again?
Where did i go wrong, its late and i cant see it.. Anyone see where?

Thanks Abydos

PS: You can see it running at.. www.shadowfleet.info/newversion/test.php
Link to comment
Share on other sites

[code]
<?php
if (!$_SESSION['username'] && !$_POST['login']){ // No session + No Form Login... Display the form...
echo "
<form action=test.php method=post name=loginform>
Username:
<br>
<input type=text name=username size=20 class=field1>
<br>
Password:
<br>
<input type=password name=password size=20 class=field1>
<br><br>
<input type=submit value=login class=button1>
<br><br>
<a href=register.php>Click Here To Register</a>
</form>
";
} else if ($_POST['username']){
//if the form has been submitted... The ifs + elses between this and next comment arnt that important just checking if the login details are correct...

$username = $_POST['username'];
$password = $_POST['password'];
//---
$result = mysql_query("SELECT * FROM sf_users WHERE username='$username'") or die(mysql_error());
$user = mysql_fetch_array( $result );
$num = mysql_num_rows($result);
if ($num > 0){
echo "<font color=\"#FF0000\"><b>Unknown username, please try again</b></font>";
} else {
$dbusername = $user['username'];
$dbpassword = $user['password'];
if ($username == $dbusername && $password == $dbpassword){
session_register("user");
$_SESSION['username'] = $username;
$_SESSION['password'] = $password;
//-- Rest Of Info On The User
$_SESSION['member_id'] =  $user['member_id'];
$_SESSION['email'] =  $user['email'];
$_SESSION['CL'] =  $user['CL'];
$_SESSION['banned'] =  $user['banned'];
//-- Session Variables End
echo "<font color=\"#FF0000\"><b>Thank you for login in,<br /> You will be redirected to the protected pages in 2 seconds <META HTTP-EQUIV=\"refresh\" CONTENT=\"2; URL=index.php\"></b></font>";
} else if ($username == $dbusername && $password != $dbpassword){
echo "<font color=\"#FF0000\"><b>Your Password is incorrect</b></font>";

} else {
echo "<font color=\"#FF0000\"><b>Unknown System Error!<br> Please ensure your Cases are correct!</b></font>";
}
};


} elseif ($_SESSION['username']){
//If we have the session... echo the username has logged in
//Display member options...
$uname = $_SESSION['username'];
echo "<font color='#FFFFFF'><b>$uname... You are already logged in.</b></font>";
} else {
//WOOPS somethings gone wrong if i see this...
echo "<font color='#FFFFFF'><b>Unknown System Error.</b></font>";
}

?>
[/code]
Link to comment
Share on other sites

Ohh That would have been very smart of me to add it in, completly forgot, Thanks..

But now that ive put that in... And still when i submit the form.. Its displaying the form again :(


EDIT:: Im connected to my database.. Im requiring a file into the top of the page with the code of..

<?php
require_once("config.php");
session_start();
conn_db();
?>

The config file has my database info and the function..
Link to comment
Share on other sites

A tip for you is to look into the md5 function to encrypt your passwords. this is one-way, so you can only compare the encrypted against each others like this:

if(md5($dbpassword) == $password) {
\\password ok.
This is of course if you saved the password with md5 to the database.

'Passwords encrypted with md5 cannot be decrypted!
Link to comment
Share on other sites

Ok great.. How exactly would i do this?
So in the registeration script (Not started:P), when inserting the user into the database use...
$userpasswd = md5($formenteredpassword);
INSERT into blah VALUES ($userpasswd...)

and then in the login script...

if(md5($dbpassword) == $password) {
\\password ok.

Is this correct?
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.