preventing direct access to a php page, only access if redirected

前端 未结 9 1355
无人及你
无人及你 2021-01-05 11:30

I want to make my php page only accessible from another page redirect and prevent my user from accessing it directly.

I mean, let\'s say I have a page called

9条回答
  •  鱼传尺愫
    2021-01-05 11:47

    To prevent access to pages, the best practice is to use session variables say $_SESSION['username'] and $_SESSION['password'] to check against your database table record assuming your table name is "users", the fields 'username' and 'password' in order for users to gain access to the page, else they are redirected to the log in page for them to supply the correct username and password through the input field.

    Below is an anatomy of Preventing Direct Access to a PHP Page.

    session_start();
    
    $username=$_POST['username'];
    $password=$_POST['password'];
    
    $query="select * from users where username='$_SESSION[username]' and     password='$_SESSION[password]'";
    
    $result=mysql_query($query);
    
    if($result)
    {
    
    echo "Your login was successful..";// the page you want to go to if login successful
    {
    else
    {
    
    header("Location:index.php?action=login");//any page you want to return to if log in failed
    }
    

提交回复
热议问题