Samza Posted April 18, 2012 Share Posted April 18, 2012 Hey I am having a small problem with setting and storing data using $_SESSION for a login script to validate that a user is logged in. The outputted error message I'm getting is; Notice: Undefined index: userid ...\...\ Notice: Undefined variable: userid in ...\...\ The PHP code I'm using for the validation <?php session_start(); //must call session_start before using any $_SESSION variables $username = $_POST['username']; $password = $_POST['password']; $userid = $_POST['userid']; //validating a user function validateUser() { session_regenerate_id (); //this is a security measure $_SESSION['valid'] = 1; $_SESSION['userid'] = $userid; } Let me know if you need any other bits of code to establish what is happening I really need help with this. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/261175-_session-data-storing-help/ Share on other sites More sharing options...
AyKay47 Posted April 18, 2012 Share Posted April 18, 2012 the undefined index error could be triggered by either $_POST or $_SESSION, give us the exact line. $userid does not exist inside of the functions scope, you must pass it in through the argument list in order to use it in the function. Quote Link to comment https://forums.phpfreaks.com/topic/261175-_session-data-storing-help/#findComment-1338431 Share on other sites More sharing options...
Samza Posted April 19, 2012 Author Share Posted April 19, 2012 Okay this is my login file <?php session_start(); //must call session_start before using any $_SESSION variables $_SESSION ['loggedin'] = 'loggedin'; $username = $_POST['username']; $password = $_POST['password']; $userid = $_POST['userid']; //validating a user function validateUser() { session_regenerate_id (); //this is a security measure $_SESSION['valid'] = 1; $_SESSION['userid'] = $userid; } //connection to the database include 'connection.php'; $username = mysql_real_escape_string($username); $query = "SELECT id, password, salt FROM users WHERE username = '$username';"; $result = mysql_query($query); if(mysql_num_rows($result) < 1) //no such user exists { header('Location: incorrectlogin.php'); die(); } $userData = mysql_fetch_array($result, MYSQL_ASSOC); $hash = hash('sha256', $userData['salt'] . hash('sha256', $password) ); if($hash != $userData['password']) //incorrect password { header('Location: incorrectlogin.php'); die(); } else { validateUser(); //sets the session data for this user } //login successful ?> Notice: Undefined index: userid in ..\..\. on line 7 $userid = $_POST['userid']; Notice: Undefined variable: userid in ..\..\. on line 14 $_SESSION['userid'] = $userid; I'm not sure why it's not working because I have declared it before the function. Quote Link to comment https://forums.phpfreaks.com/topic/261175-_session-data-storing-help/#findComment-1338697 Share on other sites More sharing options...
Muddy_Funster Posted April 19, 2012 Share Posted April 19, 2012 ... I'm not sure why it's not working because I have declared it before the function. You still need to pass it into the function, functions don't have a global scope. Think of functions like vegas : what happens in a function, stays in a function. You need to explicitly pass variables in and out of functions or it won't work. as for the $_POST['userid'] I'm guessing you don't actualy have a form element that matches that. Quote Link to comment https://forums.phpfreaks.com/topic/261175-_session-data-storing-help/#findComment-1338706 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.