Jump to content

[SOLVED] Sessions Test Not Working


graham23s

Recommended Posts

Hi Guys,

 

i normally use cokies but have been playing around with sessions, i have this basic script:

 

login.php

 

<form action="check_login.php" method="POST" />
Username:<input type="text" name="username" />
Password:<input type="password" name="password" />
<input type="submit" value="Login" />
</form>

 

check_login.php

 

<?php

session_start();

mysql_connect("localhost", "root", "xxxxxxxx");
mysql_select_db("xxxxxxxxxxxx");

$username = $_POST['username'];
$password = $_POST['password'];

$sql = "SELECT * FROM users WHERE username = '$username' AND password = '$password'";
$result = mysql_query($sql); 

if (mysql_num_rows($result) == 0) {

echo "Username and password combination not found.";

} else {

$row = mysql_fetch_array($result);

$_SESSION['username'] = $username;

header("members.php");

}  

?>

 

members.php

 

<?php
session_start();

if (!isset($_SESSION['username'])) {
   
//Not logged in, redirect to login page   

header("login.php");

} //If they pass that, they're logged in, and are authorized to view whatever else is in this file  

echo 'YOU ARE LOGGED IN';

?>

 

i was wanting to make the users , username the logged in variable so i could later on use it to drag info from mysql about them but it doesn't seem to get past check_login.php

 

what am i doing wrong?

 

thanks guys

 

Graham

Link to comment
https://forums.phpfreaks.com/topic/58146-solved-sessions-test-not-working/
Share on other sites

Hi John,

 

sorry do you mean like this:

 

<?php
session_start();

$_SESSION['username'];

mysql_connect("localhost", "root", "milkybar");
mysql_select_db("myliveaudition");

$username = $_POST['username'];
$password = $_POST['password'];

$sql = "SELECT * FROM users WHERE username = '$username' AND password = '$password'";
$result = mysql_query($sql); 

if (mysql_num_rows($result) == 0) {

echo "Username and password combination not found.";

} else {

$row = mysql_fetch_array($result);

$_SESSION['username'] = $username;

header("members.php");

}  

?>

 

cheers

 

Graham

Nevermind. You started the session already ($_SESSION['username'] = $username). I overlooked that before. Sorry.

 

Solution:

check_login.php:

<?php

session_start();

mysql_connect("localhost", "root", "xxxxxxxx");
mysql_select_db("xxxxxxxxxxxx");

$username = $_POST['username'];
$password = $_POST['password'];

$sql = "SELECT * FROM users WHERE username = '$username' AND password = '$password'";
$result = mysql_query($sql); 

if (mysql_num_rows($result) == 0) {

echo "Username and password combination not found.";

} else {
    while($row = mysql_fetch_array($result)) {
        $_SESSION['username'] = $row['username'];
    }
header("members.php");

}  

?>

Hi John,

 

thanks for that, it seems to stick at check_login.php. it doesn't goto members.php (even though everything looks good it must have found the correct credentials or it would have said otherwise)

 

is there something i have overlooked?

 

cheers

 

Graham

Archived

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

×
×
  • 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.