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:开发者_Python百科//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
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');
}
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();
}
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.
精彩评论