How to redirect users based on random URL string?

不想你离开。 提交于 2020-02-23 06:17:26

问题


For security purposes, I need to only display a page to users if they are coming from a specific site, and not direct visitors.

The way I currently have it set up, there will be a random 12-character string generated and appended to the url. For example:

http://www.example.com?skey=a72bzy321bgf

The skey parameter will always be random, but always have 12 characters. So basically, if my URL has a &key= parameter with 12 random characters, the page is displayed correctly. If not, the visitors go somewhere else.

Any help in regards to how to code this into the page, or what I should Google to find the necessary instructions? I have looked all over and haven't found how to go on about this, nor do I really know exactly what to look for. So any help would be greatly appreciated.

Thanks!


回答1:


Request the value of skey and find the length of the value.If the length is not equal to 12 then use die() to prevent the user see the page.Use as follows in the destination page.(http://www.example.com/index.php?skey=a72bzy321bgf)

<?php 
    if(!isset($_REQUEST['skey']) || strlen($_REQUEST['skey']) != 12){
        die("Soory,You came to this page in a Wrong Way.");
    }
?>
//Your Code will goes here.



回答2:


You can get key for URL and then check in your function as it is valid on starting of your page

$key = trim($_GET['key']);

if(!isValidKey($key)){
  //die('Invalid Key'); //or
  header("Location: wrongkey.html");
  exit;
}



回答3:


You need to check if $_GET['skey'] isset(), and if the string length is 12 characters.

if (isset ($_GET['skey']) && strlen($_GET['skey']) == 12) {
    // Get in
}
else {
    // Go away
}

This is of course not the correct way to check where the user came from, as your referer URL will need to have the skey set as part of the URL somehow.

Can you instead check if your $_SERVER['HTTP_REFERER'] isset()?




回答4:


Thanks everyone!

Based on all your suggestions, I combined some things and this is working perfectly for me so far:

<?php 
if(!isset($_REQUEST['skey']) || strlen($_REQUEST['skey']) != 12){
     header("Location: http://example.org/not-allowed.php");
     exit;
}
?>

Thanks! Wish I could upvote all your replies. Hope this helps others.



来源:https://stackoverflow.com/questions/29789025/how-to-redirect-users-based-on-random-url-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!