Friday 11 October 2019

PHP Tutorial | Easiest Way To Redirect To Last Visited Page After Login | No Session Or Database

index.php
<?php include('header.php');?>
<h1>This is the INDEX Page</h1>

-------------------
header.php
<?php
$protocol=$_SERVER['SERVER_PROTOCOL'];

if(strpos($protocol, "HTTPS"))
{
    $protocol="HTTPS://";
}
else
{
    $protocol="HTTP://";   
}
$redirect_link_var=$protocol.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
?>
<table width="100%" border="1" cellspacing="0">
    <tr>
        <td><a href="index.php">index</a></td>
        <td><a href="login.php?page_url=<?php echo $redirect_link_var;?>">login</a></td>
        <td><a href="page1.php">page 1</a></td>
        <td><a href="page2.php">page 2</a></td>
        <td><a href="page3.php">page 3</a></td>
    </tr>
</table>

-------------------
login.php

<?php include('header.php');?>
<?php
if($_POST)
{
    if($_POST['username']=="123"&&$_POST['password']=="123")
    {
        $redirect_link=$_REQUEST['page_url'];
        if($redirect_link=="")
        {
            header("location: index.php");
        }
        else
        {
            header("location: ".$redirect_link);   
        }
    }
}
?>
<form action="" method="post">
    <input type="text" name="username">
    <br/>
    <input type="password" name="password">
    <br/>
    <input type="submit" name="" value="login">
</form>

-------------------

page1.php
<?php include('header.php');?>
<h2>This is page 1</h2>
-------------------
page2.php
<?php include('header.php');?>
<h3>This is page 2</h3>
-------------------
page3.php
<?php include('header.php');?>
<h4>This is page 3</h4>

No comments:

Post a Comment