简体   繁体   中英

How to redirect to another page if it is accessed directly? [PHP]

How to redirect a page to the homepage if it is accessed directly (Without a variable)? Suppose we have a link "example.com/site?website=$_GET[variable]" if it is accessed like this then it is OKAY! but if someone tries to access it directly using this link "example.com/site" Then it should redirect to the Homepage. How to do it?

if(isset($_GET['variable']) && !empty(trim($_GET['variable']))) 
{
//your rest of code
} else {
header("Location:redirectpage.php");
}

You just need to check at the top of your script if the global $_GET variable is set (which it will be if any query string variables are set) and redirect if not.

if(!isset($_GET)):
   header("Location: /");
   exit(); 
endif;

You can just add that at the top of your code and if the query string is set then your code will carry on as normal.

This is supposed to be a comment but i have a low reputation here.

I am a bit confused. But why dont you just check for the existence of the variable in the link Like:

<?php

//add code below on every page or have it in a file and include everywhere $access = false;

if(isset($_GET['variable_name'])){
//obviously any one can pass any funny variable, so you will need to match it with your database to verify or the variable might be a unique word of some sorts.
   $access = true;
}else{
  $access = false;
}

if($access == false){
    die('No access to view this page');
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM