سؤال

Today my question is how do i get people to roam the site with out logging in, I have tryed loads and loads of diffrent ways, when i tried to roam my site when i was not logged in it just used to redirect me to my login page, but when i tried my most recent code (the one below this post) it just comes up with this error, Undefined index: username in E:\wamp\www\login\main.php on line 6

<?php 
ob_start();

//session
session_start();

$_session_username = $_SESSION['username'];

if (!isset($_session_username))
{
    echo"Hello i'm sorry to say this but your not logged in <a href='login.php'>Log-in</a>";
    exit();
}
else
{
    echo "hello, ".$_session_username." <a href='logout.php'>Log out</a>";
}
ob_end_flush();
?>
هل كانت مفيدة؟

المحلول

Put simply you were on the right path, however you can't assign $_SESSION['username'] to a variable and then check if it is set. You first need to check if the $_SESSION['username'] is set, and then if it is you are able to assign it to a variable.

<?php ob_start();

//session
session_start();

if (!isset($_SESSION['username']))
{
    echo"Hello i'm sorry to say this but your not logged in <a href='login.php'>Log-in</a>";
    exit();
}
else
{
    $_session_username = $_SESSION['username'];
    echo "hello, ".$_session_username." <a href='logout.php'>Log out</a>";
}
ob_end_flush();
?>
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top