Dan Dan - 10 days ago 9
PHP Question

Undefined index: $_SESSION[]

I have moved my site from local to a subdomain on a liv server and I am having some issues around my login script, it weas all working perfectly locally but it seems that sessions are not working as I expected since moving online.

So my script looks like the following:

<?php
session_start();
include_once '../config.php';

$email = mysqli_real_escape_string($conn, $_POST['email']);
$password = mysqli_real_escape_string($conn, $_POST['password']);

$query = "SELECT user_email, user_password FROM users WHERE (user_email = '". $email ."')";
$result = $conn->query($query);

if ($result->num_rows === 1) {

$row = $result->fetch_array(MYSQLI_ASSOC);

if (password_verify($password, $row['user_password'])) {

$_SESSION['user_id'] = $row[ 'user_id' ];
$_SESSION['user_email'] = $row[ 'user_email' ];

$_SESSION['video_1_status'] = $row[ 'video_1_status' ];
$_SESSION['video_2_status'] = $row[ 'video_2_status' ];
$_SESSION['video_3_status'] = $row[ 'video_3_status' ];
$_SESSION['video_4_status'] = $row[ 'video_4_status' ];

header('Location: ../home.php');

} else{
header('Location: ../login.php?message=Email address or password is incorrect');
}

} else{
header('Location: ../login.php?message=Email address or password is incorrect');
}

$conn->close();

?>


So pretty straightforward no tricks, but when I pass the values it pings me back some errors:

Notice: Undefined index: user_id in /var/sites/r/reedvid.mydomain.co.uk/public_html/actions/login-action.php on line 17

Notice: Undefined index: video_1_status in /var/sites/r/reedvid.mydomain.co.uk/public_html/actions/login-action.php on line 20

Notice: Undefined index: video_2_status in /var/sites/r/reedvid.mydomain.co.uk/public_html/actions/login-action.php on line 21

Notice: Undefined index: video_3_status in /var/sites/r/reedvid.mydomain.co.uk/public_html/actions/login-action.php on line 22

Notice: Undefined index: video_4_status in /var/sites/r/reedvid.mydomain.co.uk/public_html/actions/login-action.php on line 23

Warning: Cannot modify header information - headers already sent by (output started at /var/sites/r/reedvid.mydomain.co.uk/public_html/actions/login-action.php:17) in /var/sites/r/reedvid.mydomain.co.uk/public_html/actions/login-action.php on line 25


It is clear what it is telling me but im not entirely sure as to A: why this worked locally and B: what is wrong?

Answer

Make sure that on your webserver in your php.ini your sessions are saved right correctly. You can also try to find out if it generates a new session everytime you refresh by printing out session_id();