PHP header(Location: ) stuck in redirection loop - php

I realize there are many question on PHP headers() however I could find nothing which specifically addresses my question based on my very simple piece of code.
My Problem
So I have this very simple block of code
$url = $_SERVER['REQUEST_URI'];
$profile = explode('/', $url);
if ($profile[3] == 'index2.php') {
// echo $profile[3] prints index2.php
header('Location: profile.php');
exit();
}
As can be seen on the image below the code results in an infinite redirect loop.
when I set an absolute url like header('Location: http://localhost:63342/mvc2.0/users/profile.php'); I still get the exact same problem. When I go incognito or use a different browser I still get same problem...
If anyone can provide me with some info or suggestions to what I am doing wrong it would be greatly appreciated.

You could probably simplify that to
$profile=basename( $_SERVER['REQUEST_URI'] );
if ( $profile == 'index2.php' ) {
exit( header( 'Location: profile.php' ) );
}

Related

Header Location is not working

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.

Should this function be working? - new to php

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

SESSION isset, redirect

This is my code for userslist.php. I put it above the head of this page so if this link is clicked, only admin can enter the page as filtered that is why I have redirections.
session_start();
$loggedInfo['username'] = $_SESSION['username'];
if(
isset($loggedInfo['username']) && $loggedInfo['username']==="admin" &&
trim($loggedInfo['username']) != "guest"
)
{
header('Location: userslist.php');
}
else {
header('Location: ../index.php');
}
This is my php script and I got a problem with redirecting. On the header(location ...) when I changed it to echo true or false, the echo returns the value correctly. But when I put a redirect/location, it does say:
This webpage has a redirect loop
Why is that? :(
Put this code in top of the userlist.php.An try what you got
<?php session_start();
$loggedInfo['username'] = $_SESSION['username'];
if(isset($loggedInfo['username']) && $loggedInfo['username']!="admin"){
header('Location: ../index.php');
exit();
}else if(isset($loggedInfo['username']) && $loggedInfo['username']=="admin"){
?>
You page code here goes
<?php } ?>
You're probably including this code in all pages. Thus on userslist.php it will also redirect to userslist.php. This causes permanent redirects, which is a redirect loop.
This conclusion is however difficult to support without seeing all the code you are using.

why is my header("Location: $_SERVER['HTTP_REFERER']"); PHP function not working?

It works when I input
header("Location: http://www.google.com");
but it doesn't work when I have
header("Location: $_SERVER['HTTP_REFERER']");
I want to redirect the page to whatever page it came from.
Try it: :)
if (!empty($_SERVER['HTTP_REFERER']))
header("Location: ".$_SERVER['HTTP_REFERER']);
else
echo "No referrer.";
However, for determining which page user came from, I'd rather use session variable, which gets reset at every page:
session_start();
echo "Previous page:", $_SESSION['loc'];
$_SESSION['loc']=$_SERVER['PHP_SELF'];
ps: This only works for local pages, you cannot track other websites.
You might try:
header("Location: {$_SERVER['HTTP_REFERER']}");
I've had problems with variable expressions which contain quotes in strings without braces.
You also need to look out for $_SERVER['HTTP_REFERER'] simply not being set. Some user agents don't set it, some privary tools mask it, and you need to handle people coming to your page without a referrer.
Here is a simple solution.
check and see what $_server['http_referer'] is giving you and if its set then you can redirect and if not put a fall back url something like :
if(isset($_SERVER['HTTP_REFERER']) && $_SERVER['HTTP_REFERER'] != ""){
$url = $_SERVER['HTTP_REFERER'];
}else{
$url = "YOUR INDEX PAGE OR SOMETHING";
}
header("Location: ".$url);
This is a browser feature, and any polite browser will send the
correct header (although various 'security' tools will override this
with a fake referer).
It's browser specific so not every browser/security software combination will send it to the server. You're better off setting a session variable on each page load to determine which page the user came from (or something similar with a bit more logic)
header("Location: $_SERVER[HTTP_REFERER]");
Without the single quotes. This is the fastest way to access and concatenate array values without extra concatenating code.
Simply you can use
if(isset($_SERVER['HTTP_REFERER'])){
header("Location:".$_SERVER['HTTP_REFERER']."");
}
One of the mistakes that occure sometimes is, that NO OUTPUT must happen before header('Location: ' ....)
This is not working (shows the output, but doesn't redirect):
if (isset($_SERVER['HTTP_REFERER'])) {
$referer = $_SERVER['HTTP_REFERER'];
$cleaned_url = preg_replace('/[^a-z ]+/i', '', strtolower($referer));
$pattern = '/troester/';
$res = preg_match($pattern, $cleaned_url);
echo $res; // <--- OUTPUT COMES HERE
if ($res == true) header("Location: {$referer}");
}
This is working (does redirect properly):
if (isset($_SERVER['HTTP_REFERER'])) {
$referer = $_SERVER['HTTP_REFERER'];
$cleaned_url = preg_replace('/[^a-z ]+/i', '', strtolower($referer));
$pattern = '/troester/';
$res = preg_match($pattern, $cleaned_url);
//echo $res; // <--- NO OUTPUT COMES HERE
if ($res == true) header("Location: {$referer}");
}
This is also working, but doesn't make sense ():
if (isset($_SERVER['HTTP_REFERER'])) {
$referer = $_SERVER['HTTP_REFERER'];
$cleaned_url = preg_replace('/[^a-z ]+/i', '', strtolower($referer));
$pattern = '/troester/';
$res = preg_match($pattern, $cleaned_url);
if ($res == true) header("Location: {$referer}");
echo $res; // <--- OUTPUT COMES HERE, AFTER header('Location: ' ....)
}
(For better understandig, hope this may help)

PHP - redirection under Chrome

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

Categories