Skip to content
Advertisement

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

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 “main.php” and another PHP file that I want to prevent direct access to, called “noaccess.php”.

I want to make noaccess.php accessible only if I redirect from main.php

Any suggestions?

UPDATE: Session is a good idea, but the problem is I have to use JavaScript to redirect the page, so the question is, can I use ajax to set a PHP session?

UPDATE 2: OK I found the solution, I don’t need preventing direct access now, as I can check from mysql whether the page needs to be accessible or not.

Advertisement

Answer

What if everytime you were going to redirect you saved a value in the $_SESSION variable. So you have

//code
$_SESSION['fromMain'] = "true";
header("Location: noaccess.php");

Then in noaccess.php put

if($_SESSION['fromMain'] == "false"){
   //send them back
   header("Location: foo.php");
}
else{
   //reset the variable
   $_SESSION['fromMain'] = "false";
}

I really don’t know if this would work or not, but this is what I would try off the top of my head.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement