Script:
https://example.com/docs/index.php
In index.php, I have the following code:
header('Location: page2.php');
However, instead of opening page2.php, the following URL is opened:
https://example.com/docs/index.php/page2.php
If I put an absolute URL, everything works.
Why is this happening?
Is there any workaround so that I don't have to use an absolute URL?
<?php
/* Redirect to a different page in the current directory that was requested */
$host = $_SERVER['HTTP_HOST'];
$uri = rtrim(dirname($_SERVER['PHP_SELF']), '/\\');
$extra = 'mypage.php';
header("Location: http://$host$uri/$extra");
exit;
?>
http://php.net/manual/en/function.header.php
Related
i'm creating a index.php file for redirect all website to specific host
I'd like create a little php script that read url and redirect based on specific filter.
for example:
if url = (everything).domain1.com redir to default1.php
if url = (everything).domain2.com redir to default2.php
in all other case that not like first or second redir to default3.php
this is possible with php? i must use $_SERVER['HTTP_HOST'] or can I use other method?
i resolved with:
<?php
$domain = $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
if (strpos($domain,'domain-name.com') == true) {
header('location: /index2.php');
exit();
} else {
header('location: /index3.php');
}
?>
but not redirect...
I originally had this code (without die) but it would allow the page to be viewed.
I tried to add the die function, however it is just showing the blank page.
<?php require('dbcon.php');?>
<?php session_start();
if (!isset($_SESSION['adminauth']))
{
header('login.php');
die();
};
?>
Enable your error output with error_reporting(E_ALL) and ini_set('display_errors', 'on'); then you see all your errors. There are some things. You should start your session at the top of the script and your header is not correct.
header('Location: login.php');
Otherwise you have an error.
#Rizier123 right, but one comment - HTTP/1.1 requires an absolute URI as argument to » Location: including the scheme, hostname and absolute path, but some clients accept relative URIs.
<?php require('dbcon.php');?>
<?php
/* Redirect to a different page in the current directory that was requested */
if (!isset($_SESSION['adminauth'])) {
$host = $_SERVER['HTTP_HOST'];
$uri = rtrim(dirname($_SERVER['PHP_SELF']), '/\\');
$extra = 'login.php';
header("Location: http://$host$uri/$extra");
die();
}
?>
see PHP header DOCS:
<?php
header('Location: http://www.example.com/');
exit;
?>
$home_url = 'http://' . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']).'/homepage.php';
header('Location: '.$home_url);
I am using this redirect code for redirection from login page to home page
but
1-->directory/includes/code.php
2->directory/homepage.php
how to redirect from 1 to 2
Try this:
$home_url = '../homepage.php';
header('Location: '.$home_url);
You can do relative redirect using:
location = '../'; //one level up
location = '/path'; //relative to domain
i have the following function
function redirect_to( $location = NULL ) {
if ($location != NULL) {
header("Location: {$location}");
exit;
}
then I use it to redirect with an argument
$id = $photo->id; // integer
redirect_to("photovietnam.php?id=$id");
This work fine on my local system with wamp.
on the server the correct url is displayed in the head ,but it appears to lack a return
did anybody had the sam experiance ?
Check the docs: http://de3.php.net/manual/en/function.header.php, you might need to have an absolute path to the resource.
Note:
HTTP/1.1 requires an absolute URI as argument to » Location: including
the scheme, hostname and absolute path, but some clients accept
relative URIs. You can usually use $_SERVER['HTTP_HOST'],
$_SERVER['PHP_SELF'] and dirname() to make an absolute URI from a
relative one yourself:
<?php
/* Redirect to a different page in the current directory that was requested */
$host = $_SERVER['HTTP_HOST'];
$uri = rtrim(dirname($_SERVER['PHP_SELF']), '/\\');
$extra = 'mypage.php';
header("Location: http://$host$uri/$extra");
exit;
?>
The following header function is not working. I ma trying to go to login if the user is not logged in -
<?PHP
if (logged_in() === false) {
header('Location: login.php');
}
?>
However if I do -
<?PHP
if (logged_in() === false) {
echo"No user is logged in";
}
?>
It does echo it and I can see that it says no user is logged in
It is basically just checking if there is a user logged in
function logged_in() {
return (isset($_SESSION['user_id'])) ? true : false;
}
Try to put exit() or die() after the header like
if (logged_in() === false) {
header('Location: login.php');
exit(); //or die();
}
But makesure that your login.php should be in the same folder
Make sure that there is no output(white-space also) in your code.
you can use ob_start() and ob_end_flush() to clear out-put.
<?php ob_start();
// code
ob_end_flush(); ?>
You probably need to include the fully qualified domain and path to the new url. There is a note on the official documentation for the header function indicating as such.
Note:
HTTP/1.1 requires an absolute URI as argument to » Location: including
the scheme, hostname and absolute path, but some clients accept
relative URIs. You can usually use $_SERVER['HTTP_HOST'],
$_SERVER['PHP_SELF'] and dirname() to make an absolute URI from a
relative one yourself:
This note also contains the following code sample.
<?php
/* Redirect to a different page in the current directory that was requested */
$host = $_SERVER['HTTP_HOST'];
$uri = rtrim(dirname($_SERVER['PHP_SELF']), '/\\');
$extra = 'login.php';
header("Location: http://$host$uri/$extra");
exit;
?>