jQuerybeast
jQuerybeast

Reputation: 14510

PHP Include question

Is it possible to include a php in a website but when you refer to the website you included, then you'll be redirected. For example:

In index.php we have:

 <?php include('http://mydomain.com/aboutme.php')   ?>

but if you type http://mydomain.com/aboutme.php then you'll be redirect to index.php.

Is that possible?

Thanks

Upvotes: 0

Views: 84

Answers (3)

Shakti Singh
Shakti Singh

Reputation: 86476

Yes, it is possible check the requested file name if it is aboutme.php redirect using header

$basename = basename($_SERVER['REQUEST_URI']);
$filename = basename($basename);

if ($filename == "aboutme.php")
{
   header('location:index.php');
}

Upvotes: 2

Bryan A
Bryan A

Reputation: 3634

You may want to consider using a JavaScript redirect.

<script type="javascript">
    window.location = "index.php";
</script>

Since the header() function needs to be at the top of the PHP file, it may fail if you're including it in some other file.

Upvotes: 0

Jeff Lambert
Jeff Lambert

Reputation: 24671

It is possible to do what you want, however because HTTP is stateless you will need to employ some type of state-maintaining device, aka either Cookies or Sessions, or even variables (since you're doing an 'include' from your index..

For instance, at the top of index, before your include you can put:

$fromIndex = true;

And then at the top of your aboutme.php file you can put a simple check:

if(!isset($fromIndex) || !$fromIndex) {
    header("Location: index.php");
    exit();
}

Upvotes: 1

Related Questions