nukman nukman - 24 days ago 9
PHP Question

Prevent user to access login page if has logged in

I want to try create login page which it can be accessed by user if has not logged in, this is my code,

<?php
session_start();
if ($_SESSION['MM_Username'] != '') {
header ("Location: home.php");};
?>


It works!
But, it generates an error "Notice: Undefined index: MM_Username in C:\xampp\htdocs\msd\index.php".
What should I do now?

Answer

Check the session exists or not using isset

<?php
session_start();
if (isset($_SESSION['MM_Username'])) {
header ("Location: home.php");
}
?>
Comments