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

前端 未结 9 1345
无人及你
无人及你 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 12:08

    You tried on this Iva. Below is the code that works:

    $url != 'your-url-which-you-do-not-what-direct access';
    
    if ($_SERVER['HTTP_REFERER'] == $url) {
      header('Location: otherurl.php'); //redirect to some other page
      exit();
    }
    

    Ensure this appears at the top of the page where you do not want direct access to.

    0 讨论(0)
  • 2021-01-05 12:09

    You can use $_SERVER["HTTP_REFERER"]. Put the following code in the beginning of your php file and set $url to be equal of your desired url for example http://a.com/main.php

    if ($_SERVER['HTTP_REFERER'] != $url) {
        header('Location: noaccess.php');
        exit();
    }
    
    0 讨论(0)
  • 2021-01-05 12:12

    I think you're probably coming at the problem from the wrong direction, but if you really want to implement this I'd most likely do it with a session variable. Just have main.php set a flag indicating that they're now able to access noaccess.php and then redirect there. noaccess.php checks for the flag, and only functions if it's been set.

    0 讨论(0)
提交回复
热议问题