Jump to content

check login and put value in session


Sharkadder

Recommended Posts

Hi, basically i have a login system, the user types in their username and password, they hit the submit button, a check is done in a file called process.php to check the information is correct, if successful they are then logged in.On the login form the user has the option to press "remember me", when the user presses submit a check is done on the mysql database for the inputted data. If the details are correct the post username and password input text is then stored within 2 variables called $username and $password. If the user presses the "remember me" checkbox, the values from the $username and $password variables are then stored into a cookie upon successful login, these cookie values can then be called on other pages by checking the cookie.So my question is this:Say the user does not tick remember me, how can i store the values within a session and then call upon it on a later page? The reason i asked is, so that when people close the browser, it will log them out. Basically i want the session to equal the stored variable $userame's value. Then on a later page, i can check that the session username's value is the same as in the database (to stop people typing any old value in), if it is, then i can display it on another page.I have seen examples of this done, however they have all been within 1 long php file. The way i want is to be able to call upon the session on extrnal php pages, the way's i have tried never worked, here is my last attempt: $_SESSION['user'] = "$username"; //set session to equal username variablethen on another page i tried echoing that session, no joy.The current process.php file (the one the login page calls upon when the user presses "submit" within the login form) is shown here:<?phpsession_start(); // Shows we are using sessions$dbHost = '*****';$dbUser = '*****';$dbPass = '*****';$dbname = '*****'; $username = $_POST['username']; // Gets the inputted username from the form$password = $_POST['password']; // Gets the inputted password from the form$time = time(); // Gets the current server time$check = $_POST['setcookie']; // Checks if the remember me button was ticked$db = mysql_connect($dbHost,$dbUser,$dbPass); // Connection Codemysql_select_db($dbname,$db); // Connects to database$query = "SELECT Username, Password FROM dreamboxsales WHERE Username = '$username' AND Password = '$password'";$result = mysql_query($query, $db);if(mysql_num_rows($result)) { // If the username and password are correct do the following; $_SESSION['loggedin'] = 1; // Sets the session 'loggedin' to 1 $_SESSION['user'] = "$username"; //set session to equal username variable if($check) { // Check to see if the 'setcookie' box was ticked to remember the user setcookie("dreamboxsales[username]", $username, $time + 3600); // Sets the cookie username setcookie("dreamboxsales[password]", $password, $time + 3600); // Sets the cookie password } session_start(); if (isset($_SESSION['link'])) { header('Location:'. $_SESSION['link']); exit(); } else { header('Location: http://www.sharkadder.com/dreamboxsales'); exit(); }}else // If login is unsuccessful forwards the user back to the index page with an error{ header('Location: http://www.sharkadder.com/dreamboxsales/lo...?error=1'); exit();}?>As you can see, i need a way of getting and displaying the $_SESSION['user']'s value on another page, once it equals the $username variable. I've also tried registering variables, but again, on other pages they never displayThanks

Link to comment
Share on other sites

I do use session start, because it works when i check that the user is logged in.I do a session to check if the user is logged in, but yet when i try telling it to equal a value like a string from the process file and echo it on another page, nothing happens.I'll post my few lines of code to se if it helpsEDITThat's strange it is now working, all i did was rewrite my code and it's now working, very odd.Anyways thanks ofr the help, this issue now sorted

Link to comment
Share on other sites

Archived

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

×
×
  • Create New...