Jump to content


Photo

Session login system does not work...


  • Please log in to reply
6 replies to this topic

#1 t.bo

t.bo
  • Members
  • PipPipPip
  • Advanced Member
  • 57 posts

Posted 17 July 2006 - 11:21 AM

Hi all...

I just tried my first code using sessions but there is something wrong. I always get "Your login is wrong" while I enter the correct username and pw. The username is correctly inputted though...

Here is the code Login.htm:
<form action="login.php" method="post">
<b>Username</b>:<input type="text" name="username" size="20"><br>
<b>Password</b>:<input type="password" name="pw" size="20"><br>
<input type="submit" value="Login"></form>

Login.php:
<?php
session_start();
include('dbconnect.php');
$username = $_GET[username];
$pw = $_GET[pw];
$q="SELECT * FROM `login` WHERE ((username='$username') AND (pw='$pw'))";
$result= mysql_query($q) or die ("Could not execute query : $q." . mysql_error());

if (mysql_num_rows($result) == 0)
{
echo "<div align=center><b>Your login is wrong. Please click back and try again.</b></div>";
}
else
{
$r=mysql_fetch_array($result);
$login_username=$r["username"];
session_register("login_username");
Header("Location: protected.php");
}
?>

and finally protected.php
<?php
session_start();
if ($_SESSION["login_username"]=="") {
Header("Location: login.htm");
} else {

echo "U r logged in...";
}

?>

Hope someone can help me out...
Grtz and thanks in advance

#2 hvle

hvle
  • Members
  • PipPipPip
  • Advanced Member
  • 667 posts
  • Locationmelbourne, Australia

Posted 17 July 2006 - 11:29 AM

in your login.php, change the following 2 lines:

$login_username=$r["username"];
session_register("login_username");

to

$_SESSION['login_username'] = $r['username'];


post again if it doesn't work.
Life's too short for arguing.

#3 brown2005

brown2005
  • Members
  • PipPipPip
  • Advanced Member
  • 943 posts

Posted 17 July 2006 - 11:32 AM

SELECT * FROM login WHERE username='$username' AND pw='$pw';";

try that

#4 hvle

hvle
  • Members
  • PipPipPip
  • Advanced Member
  • 667 posts
  • Locationmelbourne, Australia

Posted 17 July 2006 - 11:32 AM

don't mind my previous post, I think the error is here:

$q="SELECT * FROM `login` WHERE ((username='$username') AND (pw='$pw'))";

change to:

$q="SELECT * FROM `login` WHERE username='$username' and pw='$pw'";

Life's too short for arguing.

#5 brown2005

brown2005
  • Members
  • PipPipPip
  • Advanced Member
  • 943 posts

Posted 17 July 2006 - 11:33 AM

yeah i agree with hvle, even though i was first.. lol

#6 hvle

hvle
  • Members
  • PipPipPip
  • Advanced Member
  • 667 posts
  • Locationmelbourne, Australia

Posted 17 July 2006 - 11:42 AM

lol, u beat me to it.  >:(
Life's too short for arguing.

#7 t.bo

t.bo
  • Members
  • PipPipPip
  • Advanced Member
  • 57 posts

Posted 17 July 2006 - 01:49 PM

Thanks guys It works fine now.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users