Jump to content

[SOLVED] Session still isnt working :(


SirChick

Recommended Posts

I been trying to get my session to work from when the user logs in.. but for the life of me.. it won't load up data on other pages... i did a print of the session and it didn't pick up the User ID so im guessing its relating to the creation of the session on my login page which i have provided below.

There's no reason to suggest that it shouldn't work so I'm kinda stuck.

 

<?

//when login is pushed run code
// Start up the session
session_start();
if (isset($_POST['Login']))
{
    //connect to database as par usual
    // Needed to move these lines above the mysql_real_escape() calls
    mysql_connect("localhost", "root", "Private") or die (mysql_error());
    mysql_select_db("civilian") or die (mysql_error());

    //set input from user into these variables
    $Username = mysql_real_escape_string($_POST['Username']);
    $Password = mysql_real_escape_string($_POST['Password']);

//check if username exists
$chkLogin = mysql_query("SELECT * FROM `userregistration` WHERE `Username` = '$Username' AND Password = '$Password'");
	if (!($row = mysql_fetch_assoc($chkLogin))) {
	die('Username or password was incorrect. Please press the back button on your browser and try again!');
}
// Store the currently logged in user
$_SESSION['Current_User'] = $row['Userid'];
header("Location: HomeLogin.php");
}
    

?>

Link to comment
https://forums.phpfreaks.com/topic/64845-solved-session-still-isnt-working/
Share on other sites

Try this:

<?php

//when login is pushed run code
// Start up the session

session_start();

if (isset($_POST['Login']))
{
//connect to database as par usual
// Needed to move these lines above the mysql_real_escape() calls
mysql_connect("localhost", "root", "Private") or die (mysql_error());
mysql_select_db("civilian") or die (mysql_error());

//set input from user into these variables
$Username = mysql_real_escape_string($_POST['Username']);
$Password = mysql_real_escape_string($_POST['Password']);

//check if username exists
$chkLogin = mysql_query("SELECT * FROM `userregistration` WHERE `Username` = '$Username' AND Password = '$Password'");
$row = mysql_fetch_assoc($chkLogin);

if (!$row['Userid']) {
	die('Username or password was incorrect. Please press the back button on your browser and try again!');
}

// Store the currently logged in user
$_SESSION['Current_User'] = $row['Userid'];
header("Location: HomeLogin.php");
}
?>

 

Edit: ;

Why did you change the query? Because that worked before, its the session creation itself thats going wrong.

 

 

This occurs now:

 

Username or password was incorrect. Please press the back button on your browser and try again!

 

 

Even if the data is correct. Let me know thanks :)

UserID is the exact word and its not null. Is it case sensitive?

 

Yes, It is case sensitive.

 

Make sure that the session.save_path variable in your php.ini file points to a valid temporary directory for your system.  Also, try moving the session_start() above the comments

 

Comments does not get phrased by php.

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.