I have a problem with the following code:
<?php
session_start();
require_once("config.php");
if(isset($_SESSION['location']) && !empty($_SESSION['location'])) {
$location = $_SESSION['location'];
$url = ABS_PATH . $location;
unset($_SESSION['location']);
header('Location: ' . $url);
}
The value of $url is:
http://www.domain.eu/somepage
and value passed to header() is:
Location: http://www.domain.eu/somepage
It is ok under Opera, IE (7,8,9) and Firefox but unfortunately it is not working under Chrome and I have no idea why. Mentioned code sample comes from file index.php and it is execuded every time You load a page. I have other file (display.php) to which I send url via AJAX request. File display.php sets value of $_SESSION['location'] and returns content of page. In browsers other than Chrome when I refresh page it redirects me to proper URL. During debuging I noticed that even when I put die(); after
unset($_SESSION['location']);
it does not execute it, but when i put it before it works. Can anyone have any idea how to solve my problem?
Thanks in advance for help.
<?
session_start();
$_SESSION['location'] = 'http://www.google.com';
if(isset($_SESSION['location']) && !empty($_SESSION['location'])) {
$location = $_SESSION['location'];
header("location: ".$location);
}
?>
Works for me. Expand on that code and see when it breaks.
Add a die; right after your header:location. This should do the trick.
Chrome needs an http status first:
header("Status: 200");
header('Location:' . $url);
Related
I hope you are doing great.
i'm using header("Location...") to redirect (traverse) from one page in my website to another apparently. The problem is that it redirects me to the same page. I checked my code and don't know where is the mistake. I wonder if you could help me with it.
Here is the useful pieces of my code.
<?php
if (isset($_POST['submitted'])) {
include 'Search.php';
$url = absolute_url('Search.php');
header("Location: $url");
}
?>
Thanks in advance guys. Cheers,
Try this:
echo $url; exit; // To see the value of $url
ob_start();
header("Location: " . "http://" . $_SERVER['HTTP_HOST'] . $url);
Hope this helps.
Peace! xD
use this one it may help you ,
if (isset($_POST['submitted'])) {
include 'Search.php';
$url = 'http://'.$_SERVER['HTTP_HOST'].dirname($_SERVER['PHP_SELF'])."/Search.php";
header("location:$url") ;
}
I have searched for a while now, and it seems that I can't find a solution to my problem. I'm hoping that you guys can help me out.
This is my code:
<?php
ob_start();
if(isset($_POST['searchstring'])){
include ("connect.php");
$queried = $_POST['searchstring'];
$queried = trim($queried);
$patterns = array("/\s+/", "/\s([?.!])/");
$replacer = array("+","$1");
$queried = preg_replace( $patterns, $replacer, $queried );
header("Location: index.php?page=search&q=".$queried."");
}
else {
header('Location: index.php');
}
exit();
?>
I have even tried to only use following code:
<?php header('Location: index.php'); ?>
That's not working either. It's pretty wierd, because I have used header location a million times. It works 100% fine on localhost, but on the server it fails - it just shows a blank page. Any ideas?
use the full url instead of index.php.
Try with url like [http://example.com/index.php]
Okay. I found the error. Apparently I had somehow saved my document with BOM, which made the code somehow invalid.
Thank you all for your help.
I would like to know how to allow http refer from more than 1 site.
For example
<?php
$domain='example.net';
$referrer = $_SERVER['HTTP_REFERER'];
if (#preg_match("/example.net/",$referrer)) {
} else {
header('Location: http://www.example.net/404.php');
};
?>
This code works if I open links from example.net, but I want to allow example1.net and example2.net as well to access the links.
How do I do this? If anyone could help me with this, it would highly be appreciated.
Use the regex operator for or -- | (pipe)
<?php
$domains = Array(
'example.net',
'example2.net'
);
$referrer = parse_url($_SERVER['HTTP_REFERER'], PHP_URL_HOST); // Gets the domain name from referer
if (!preg_match("/" . implode('|', $domains) . "/", $referrer)) {
header('Location: http://www.example.net/404.php');
exit(0); //force exit script after header redirect to ensure no further code is executed.
};
// Normal code execution here...
?>
I have function within my login script that is causing me some problems. I believe this is the issue.
function load( $page = 'login.php')
{
$url = 'http://' . $SERVER['HTTP_HOST'] .
dirname( $_SERVER ['PHP_SELF'] );
$url = rtrim( $url , '/\\' );
$url = '/' . $page ;
header ( "location: $url" );
exit();
}
My users are in the db and can register fine - when using my login form I have a login form a script to check they have entered the info and also another one to validate the users. The problem being that when I/They attempt to login the action script or the logic dosnt move on it sits on that blank page...
Thats the white page of death :) There is an error, but it doesnt get shown. You can try adding this to the top:
error_reporting(E_ALL|E_STRICT);
That should give you the error on line 5 in this code, the $SERVER needs to be $_SERVER
To improve your code, this is valid too:
header('Location: /');
Relative url's. That piece of code will go to home, you can just to to your $page directly:
header('Location: /'.$page);
function load($page = "login.php") {
header(sprintf("Location: http://%s/%s/%s", $_SERVER["HTTP_HOST"], trim(dirname($_SERVER["PHP_SELF"]), "/"), $page));
exit();
}
I'd like to have my website redirect to the previous page after submitting login info.
I have searched around for this problem
I have echoed the contents of $url and even did strcmp and it evaluates true (not shown here)
Problem: The ELSE statement always evaluates even though $url == mlbmain.php OR course-website.php
Any suggestions?
<?PHP
require_once("./include/membersite_config.php");
echo "</br> </br> </br> </br>";
$url = isset($_GET['return_url']) ? $_GET['return_url'] : 'login.php';
//url now == to /mlbmain.php OR /course-website.php
$url = substr($url,1);
//url now == to mlbmain.php OR course-website.php
echo $url; //Just to make sure
$url = trim($url); //trim it to make sure no whitespaces
echo "</br>";
echo $url; //Just to make sure it's still the same
if(isset($_POST['submitted']))
{
if($fgmembersite->Login())
{
if($url == "mlbmain.php"){
$fgmembersite->RedirectToURL("mlbmain.php");
}
else if($url == "course-website.php"){
$fgmembersite->RedirectToURL("course-website.php");
}
else
$fgmembersite->RedirectToURL("index.php");
}
}
?>
After you press the Submit button you are making a POST request and the return_url variable will not be available anymore which was set with a GET request. You could create a hidden input field that will store the redirect_url and submit it with the form.
Since you say
It seems to be going to index.php by default
The problem is probably either with
if(isset($_POST['submitted']))
or
if($fgmembersite->Login())
and not related to $url at all.
I guess it can not find mlbmain.php or course-website.php at the current folder, so it throws 404 not found an probably you managed this error to redirect to index.php