Search code examples
phpsessionsession-cookiessession-variablessession-state

How can I redirect users to different pages based on session in PHP?


im trying after put username and password redirect to other page named panel.php but after the put currect values in form destination page redirect me again to login form. my login code ->

<?php
session_start();
$login='
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>login</title>
    </head>
    <body>
        <h1> login form</h1>
        <pre>
        <form method="POST" action="panel.php" >
            <input type="text" name="username">
            <input type="password" name="password">
            <input type="submit" name="submit">
        </form>  
    </body>
</html>
';
if(isset($_POST['username']) && isset($_POST['password'])){
    $username= $_POST['username'];
    $password= $_POST['password'];
    if ($username == 'teddy' && $password == '123'){
        $_SESSION['X'] = true;
        $_SESSION['username'] = $username;
        header('location: panel.php');
        exit();
    }
    else{
        echo "invalid credentials";
    }
}
else
{
    echo $login;
}
?>

and panel code ->

<?php
session_start();
$panel = '
<!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>panel</title>
    </head>
    <body>
        <p>welcom to the panel; dear ' . $_SESSION['username'] . ' </p>
    </body>
</html>
';
if ($_SESSION['X'] === true){
    echo $panel;
}
else{
    header('location: login.php');
}
?>                                                                                                                                                                                                                                                                                                                                             

i try to see panel page with session


Solution

  • The action for your login form is "panel.php" but should be "login.php". By sending the data to panel.php, login.php never gets the chance to process the post data and set the SESSION values and with unset SESSION values, panel.php redirects back to login.php.

    login.php

    <?php
    session_start();
    $login='<!DOCTYPE html>   <html lang="en">  <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0">   <title>login</title>    </head> <body>  <h1> login form</h1>   <pre>  <form method="POST" action="login.php" >   <input type="text" name="username">   <input type="password" name="password">    <input type="submit" name="submit">   </form></body> </html>';
    if(isset($_POST['username']) && isset($_POST['password'])) {
        $username= $_POST['username'];
        $password= $_POST['password'];
        if ($username == 'teddy' && $password == '123'){
            $_SESSION['X'] = true; 
            $_SESSION['username'] = $username;
            header('location: panel.php');
            exit();
        } else { 
            echo "invalid credentials";  
        }
    } else { 
        echo $login; 
    }
    

    panel.php

    <?php
    session_start();
    $panel = '<!DOCTYPE html>   <html lang="en">  <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0">   <title>panel</title>    </head> <body>   <p>welcom to the panel; dear ' . $_SESSION['username'] . ' </p></body> </html>';
    if ($_SESSION['X'] === true) {
        echo $panel;
    } else {
        header('location: login.php'); 
    }