How to redirect to last visited page after login in PHP?

Viewed 36

I'm a beginner in PHP. So I want to know how a user can see his/her last visited page after login. For example : After login, User redirects to index.php page. There is a second page named "second.php". The user visits "second.php" & logs out. Now if the user logs in next time. He/She should see the "second.php" page instead of index.php. I read some answers & it was not clear for my basic knowledge. If someone could tell me what & where exactly I should add the lines of code to work, I would be grateful. Here is my full code & all pages.

**login.php**

<?php

session_start();

    include("connection.php");
    include("functions.php");

    if ($_SERVER['REQUEST_METHOD'] == "POST") 
    {
        // something was posted
        $user_name = $_POST['user_name'];
        $password = $_POST['password'];

        if (!empty($user_name) && !empty($password) && !is_numeric($user_name)) 
        {
            // read from database
            $query = "select * from users where user_name = '$user_name' limit 1";
            $result = mysqli_query($con, $query);

            if ($result) {
                if($result && mysqli_num_rows($result) > 0)
                {
                    $user_data = mysqli_fetch_assoc($result);
                    if ($user_data['password'] === $password) 
                    {
                        $_SESSION['user_id'] = $user_data['user_id'];
                        header("Location: index.php");
                        die;
                    }
                }
            }
            echo "Wrong username or password";
        } else 
        {
            echo "Please enter some valid information!";
        }
    }


?>

<!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>
    
    <style>
        #text{
            height: 25px;
            border-radius: 5px;
            padding: 4px;
            border: solid thin #aaa;
            width: 100%;
            }
            #button{
            padding: 10px;
            width: 100px;
            color: white;
            background-color: lightblue;
            border: none;
            }
            #box{
            background-color: grey;
            margin: auto;
            width: 300px;
            padding: 20px;
        }
    </style>

    <div id="box">

        <form method="post">
            <div style="font-size: 20px; margin: 10px; color: white">Login</div>
            <input id="text" type="text" name="user_name"> <br><br>
            <input id="text" type="password" name="password"><br><br>

            <input id="button" type="submit" value="Login"><br><br>

            <a href="signup.php">Click to Signup</a><br><br>
        </form>

    </div>
</body>
</html>

**signup.php**

    <?php
    session_start();
    
        include("connection.php");
        include("functions.php");
    
        if ($_SERVER['REQUEST_METHOD'] == "POST") 
        {
            // something was posted
            $user_name = $_POST['user_name'];
            $password = $_POST['password'];
    
            if (!empty($user_name) && !empty($password) && !is_numeric($user_name)) 
            {
                // save to database
                $user_id = random_num(20);
                $query = "insert into users (user_id,user_name,password) values ('$user_id','$user_name','$password')";
                mysqli_query($con, $query);
    
                header("Location: login.php");
                die;
            } else 
            {
                echo "Please enter some valid information!";
            }
        }
    
    ?>
    
    <!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>Signup</title>
    </head>
    <body>
        
        <style>
            #text{
                height: 25px;
                border-radius: 5px;
                padding: 4px;
                border: solid thin #aaa;
                width: 100%;
                }
                #button{
                padding: 10px;
                width: 100px;
                color: white;
                background-color: lightblue;
                border: none;
                }
                #box{
                background-color: grey;
                margin: auto;
                width: 300px;
                padding: 20px;
            }
        </style>
    
        <div id="box">
    
            <form method="post">
                <div style="font-size: 20px; margin: 10px; color: white">Signup</div>
                <input id="text" type="text" name="user_name"> <br><br>
                <input id="text" type="password" name="password"><br><br>
    
                <input id="button" type="submit" value="Signup"><br><br>
    
                <a href="login.php">Click to Login</a><br><br>
            </form>
    
        </div>
    </body>
    </html>

**index.php**

    <?php
    session_start();
    
        include("connection.php");
        include("functions.php");
    
        $user_data = check_login($con);
    
    ?>
    
    <!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>My Website</title>
    </head>
    <body>
    
        <a href="logout.php">Logout</a>
        <h1>This is the index page</h1>
        <a href="second.php">Second Page</a>
    
        <br>
        Hello, <?php echo $user_data['user_name']; ?>
    </body>
    </html>

**second.php**

<?php
session_start();

    include("connection.php");
    include("functions.php");

    $user_data = check_login($con);

?>

<!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>My Website</title>
</head>
<body>

    <a href="logout.php">Logout</a>
    <h1>This is the second page</h1>
    <a href="second.php">Second Page</a>

    <br>
    Hello, <?php echo $user_data['user_name']; ?>
</body>
</html>

**connection.php**

    <?php
    
    $dbhost = "localhost";
    $dbuser = "root";
    $dbpass = "";
    $dbname = "login_sample_db";
    
    if (!$con = mysqli_connect($dbhost,$dbuser,$dbpass,$dbname,3307))
    {
        die("failed to connect!");
    }
    
    ?>

**functions.php**

    <?php
    
    function check_login($con) 
    {
        if(isset($_SESSION['user_id']))
        {
            $id = $_SESSION['user_id'];
            $query = "select * from users where user_id = '$id' limit 1";
            $result = mysqli_query($con, $query);
            if($result && mysqli_num_rows($result) > 0)
            {
                $user_data = mysqli_fetch_assoc($result);
                return $user_data;
            }
        }
    
        // redirect to login
        header("Location: login.php");
        die;
    }
    
    function random_num($length) 
    {
        $text = "";
        if ($length < 5) {
            $length = 5;
        }
    
        $len = rand(4, $length);
    
        for ($i=0; $i < $len; $i++) { 
            $text .= rand(0,9);
        }
    
        return $text;
    
    }
    
    ?>

**logout.php**

    <?php
    
    session_start();
    
    if (isset($_SESSION['user_id'])) 
    {
        unset($_SESSION['user_id']);
    }
    
    header("Location: login.php");
    die;
    
    ?>
0 Answers
Related