Jump to content

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/261175-_session-data-storing-help/
Share on other sites

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.

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.

...

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.

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.