Jump to content

Archived

This topic is now archived and is closed to further replies.

t.bo

Session login system does not work...

Recommended Posts

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:
[code]<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>[/code]

Login.php:
[code]<?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");
}
?>[/code]

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

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

?>[/code]

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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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'";

Share this post


Link to post
Share on other sites
lol, u beat me to it.  >:(

Share this post


Link to post
Share on other sites

×

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.