Andrew
Andrew

Reputation: 25

How to redirect users based on random URL string?

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!

Upvotes: 1

Views: 769

Answers (4)

Andrew
Andrew

Reputation: 25

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.

Upvotes: 1

Sutandiono
Sutandiono

Reputation: 1750

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()?

Upvotes: 1

Pravat Kumar Sahoo
Pravat Kumar Sahoo

Reputation: 303

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.

Upvotes: 1

Faizan Ayubi
Faizan Ayubi

Reputation: 59

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;
}

Upvotes: 1

Related Questions