php header does not work properly - php

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;
?>

Related

Variable php redirect based on url

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...

php - header location - wrong url opening

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

PHP Url Routing separate Dir

I'm looking for a PHP router for my website and I have my files in the root directory; For example: index.php, details.php, signup.php, recover.php
So I want details.php, signup.php and recover.php in a directory named pages and then to access the links instead on mysite.com/details.php to mysite.com/details or mysite.com/?page=details... I realy want to put them in a separate folder than root.
Thanks everyone !
You simply want an MVC.
I highly suggest you to follow this tutorial series. No frameworks used, yet working.
https://www.youtube.com/watch?v=OsCTzGASImQ&list=PLfdtiltiRHWGXVHXX09fxXDi-DqInchFD
I wrote a script years ago, and I think it might well suit your needs.
Notes:
The script is controlled by the pattern location at the top.I modified it (just inserted pages/ in the pattern) to suit your case, I hope.
You could replace all the files you want to move down to the directory page by this script - without any modification.
The script handles most of the URI elements automatically, including port numbers. But: it does not handle log-in credentials (username and password) as many modern browsers and servers do not honor these.
Also this script is copyrighted it is freeware. So you are allowed to use it in non-commercial and commercial environments.
Code
<?php
/*
---------------------------------------------------------------------------
Redirector Script
Copyright (C) 2008, methodica.ch, http://www.methodica.ch
Purpose: Redirect GET request according the pattern 'location'
Pattern variables (embraced in curly brackets):
prot: Protocol (i.e. http | https)
host: Hostname (e.g. www.example.org)
port: Portnumber (80 | 443 | 81)
path: Path to the file (includes trailing '/', might be empty)
file: Filename
quer: Query, GET parameters (e.g. 'id=myId&mode=1&close=automatic')
Other text is taken literally
---------------------------------------------------------------------------
*/
define('location','{prot}://{host}{port}/{path}pages/{file}{quer}');
// Do not modify the code below! -------------------------------------------
// Get URI elements
$prot = ((isset($_SERVER['HTTPS'])) && (strtoupper($_SERVER['HTTPS']) == 'ON')) ? 'https' : 'http';
$server_name = getenv('SERVER_NAME');
$script_url = $prot . '://' . $server_name . ':' . getenv('SERVER_PORT') . $_SERVER['PHP_SELF'];
$xres = preg_match_all('%^(http[s]{0,1})://(.*?):[\d]*?/(.*/)*(.*)$%i', $script_url, $res, PREG_PATTERN_ORDER);
if (($xres!==false) && ($xres>0)) {
// Prepare variables
$prot = $res[1];
$host = $res[2];
$path = $res[3];
$file = $res[4];
$quer = $_SERVER['QUERY_STRING'];
$xport = getenv('SERVER_PORT');
$port = ($prot=='https') ? ( ($xport=='443') ? '' : ":$xport" ) : ( ($xport=='80') ? '' : ":$xport" );
$location = location;
// Replace variable references in pattern
preg_match_all('/\{.*?\}/i', $location, $res, PREG_PATTERN_ORDER);
for ($i = 0; $i < count($res[0]); $i++) {
$varname = str_replace(array('{','}'),'',$res[0][$i]);
$srce = $res[0][$i];
$dest = $$varname;
if (is_array($dest)) $dest = ($dest[0]);
if ($srce=='{quer}') $dest = '?'.$dest;
$location = str_replace($srce,$dest,$location);
}
// Send redirection header
header("Location: $location");
exit();
} else {
// Something went awfully wrong
echo "ERROR: Cannot parse URI:<br />'$script_url'";
exit();
}
?>

PHP session if !isset {} else die()

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;
?>

PHP header function is not working

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;
?>

Categories