Jump to content

Session login system does not work...


t.bo

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
Link to comment
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.
Link to comment
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'";
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.