123
123

Reputation: 119

Is there a way to prevent the post viewed by the visitor?

Supposed the page is example.com/blog/data.php. I am using file_get_contents to get the content in another script page. Now, i want to:

  1. Forbid google search to crawl and index the data.php page.
  2. Forbid the visitor to access it

Is there a way to achieve this?

Upvotes: 2

Views: 56

Answers (4)

jeroen
jeroen

Reputation: 91742

You can redirect to another page if the request url is example.com/blog/data.php, but a far easier and more logical solution would be to move the file out of your web-root.

Edit: If you really want to keep the file inside the web-root, you can use something like this at the top of the script that you don't want to access directly:

if ($_SERVER['REQUEST_URI'] === $_SERVER['SCRIPT_NAME'])
{
  header('Location: /');    // redirect to home page
}

However, this will probably not work in combination with file_get_contents (you need to remove these lines from the result), you could include the file instead.

Upvotes: 2

SaidbakR
SaidbakR

Reputation: 13544

Simply apply access restriction for authorized users only. You are able to do it in the most simple way by accessing your page using url parama as password:

example.com/blog/data.php?secret=someblah

and in the first of your file data.php do the following:

<?php
if (!isset($_GET['secret']) || $_GET['secret'] != 'someblah')) exit();
?>

However,It is recommended, don't use this from public computer becuase it is not secure but it is the primitive authentication principle.

Upvotes: 0

banzsh
banzsh

Reputation: 550

You can pass token via GET. Overall your way is slightly wrong. Why don't you incorporate the data.php logic in the script that is calling it.

Upvotes: 0

Quentin
Quentin

Reputation: 943922

Don't put data.php under the web root. Keep it in a parallel directory.

Upvotes: 1

Related Questions