0

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

4
  • 1
    This is possible, but you usually want to avoid remote includes. Why are you doing it in the first place? Is the include on your own domain? Commented Apr 25, 2011 at 15:32
  • Remember that include is intended to load PHP code for execution. When you include a file via URL like that, what you'll get is the OUTPUT of the remote script, not its source code. Unless aboutme.php outputs PHP code, you'll most likely get syntax errors. Commented Apr 25, 2011 at 15:33
  • Yes it is. Well in the aboutme.php would be just an input so I would like people not to be able to view that on its own. Thanks Commented Apr 25, 2011 at 15:34
  • If i use iframe then people can right click see just the frame where an input will stand by itself in the page Commented Apr 25, 2011 at 15:36

3 Answers 3

2

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');
}
Sign up to request clarification or add additional context in comments.

4 Comments

I'm not sure if this still applies, but make sure to place that line of code at the very top of the file doing the redirecting.
if i use the redirection though, will i be able to include it?
@Plan: Yes, It's true you should place that condition and code at the very top of file
@rallyboy: Yes sure, you will be able. because redirect occur only in the condtion if the url is http://mydomain.com/aboutme.php
1

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();
}

1 Comment

Perfect, it looks like its what I was looking for
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.