Search code examples
phpphp-parse-error

Show a message if user is not logged in


My home page has an link to the profile page (profile.php). The profile page can only be seen by the users who are logged in. If a user clicks the profile link before log in he/she will be redirected to the login page (login.php) and a message ($mssg) will appear on the top of the log in form. --- this is what I want.

What is wrong in my observation: The header function can't seem to get the $mssg along with the page location.

profile.php:

<?php
session_start();
if(empty($_SESSION['valid'])){
$_SESSION['intruder']="stranger";
header('Location: login.php?$mssg=" You are not logged in. Please log in to see the profile. "');
}
?> 

login.php

<?php
session_start();
if (empty($_SESSION['intruder'])) //print nothing
else {
  echo $mssg;
}
// log in form code, email, password etc.
?>

The error I'm geting:

Parse error: syntax error, unexpected 'else' (T_ELSE) in C:\xampp\htdocs\sss\login.php on line 4

Why is it not working? Do you think my code is wrong? If so, then how can I fix it or get what I want?


Solution

  • You have some errors:

    $mssg = urlencode('Your message here');
    header('Location: login.php?mssg=' . $mssg);
    

    Then:

    echo $_GET['mssg'];