Jump to content

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.

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.