Allow access to php file only if redirected

Asked By 0 points N/A Posted on -
qa-featured

Is it possible to disallow direct access to a PHP file and allow the access only if it's redirected from other PHP file? For example, access to loading.php should be only allowed if it's redirected from example.php page. How how can I do that? I hope you understand what I mean. If not, please ask me, and I will try to explain better.

SHARE
Answered By 0 points N/A #188030

Allow access to php file only if redirected

qa-featured

Hi,

You can use $_SESSION in PHP.

session_start();
 
if (isset($_SESSION['userid']) /* or something like that */)
{                     
    session_unset();
    session_destroy();
    header("Location: login.php");
    exit();
}
 
// do whateven you need to do and set up $_SESSION variables 
// for example get the user entered info here
 
// This is how you set session variables
 
$_SESSION['userid']    = ...;
 
// Then after you did the registration part or something else you wanted to do
// You can redirect the user to any page you want
header("Location: other_page.php");

Every time user tries to enter the updateprofile.php right away, he or she will be redirected to login after they have logged out because the session is destroyed.

 

Related Questions