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.
Related
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' ) );
}
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 am trying to display an error message when there is a username-password mismatch. I set a php session variable if username and password dont match. Then i header back to the same page, with an if conditioned php statement to display an error if the variable is set. But when i unset the variable after error display, there is no error displayed on the page.
I have seen similar problems mentioned in this forum. But i seem to be doing everything right as suggested in questions.. Please help me out...
This is part of my code flow...
<?php
ob_start();
session_start();
.
.
if ($result = $sth->fetch(PDO::FETCH_ASSOC)){
$_SESSION['admin_user'] = $result['id'];
header('Location: admin_user.php');
} else {
$_SESSION['user_found'] = 0;
header('Location: index.php');
}
.
.
//in html body
<?php
if (isset($_SESSION['user_found'])){
if($_SESSION['user_found'] == 0){
?>
<div>
<p class = "bg-danger text-danger">Username Password Mismatch</p>
</div>
<?php
unset($_SESSION['user_found']);
}
}
?>
Now, if unset is removed..it works fine. If it is there, there is no display of error message.
Try not reloading the same page.. remove the header redirect.
if ($result = $sth->fetch(PDO::FETCH_ASSOC)){
$_SESSION['admin_user'] = $result['id'];
header('Location: admin_user.php');
} else {
$_SESSION['user_found'] = 0;
//header('Location: index.php');
}
When I tried the your code, things seem to work fine. Something should be wrong with the code you've not mentioned here..
To troubleshoot the problem instead of
unset($_SESSION['user_found']);
try changing the value of the variable.. say
$_SESSION['user_found'] = -1;
I cannot figure this out. Sometimes after the redirect (see code below), the session variables are lost. Any ideas?
Note the script is initially called with ?p=1&u=2&k=3.
As you can see, the script redirects to itself. The session variables something are lost after the redirect.
<?php
session_start();
if ((isset($_SESSION['p'])) and ($_SESSION['p'] != "")) {
// do something
} else {
$_SESSION['p'] = $_GET['p'];
$_SESSION['w'] = $_SERVER["HTTP_HOST"];
$_SESSION['u'] = $_GET['u'];
$_SESSION['k'] = $_GET['k'];
header("Location: http://".$_SESSION['w'].$_SERVER['PHP_SELF']."");
exit();
}
?>
Cheers
Copied and pasted your code and it works just fine for me.
Do you maybe have some spaces or whatever before your <?php-tag?
I am not sure why it happens.
Probably you have some misconfiguration on your php.ini file.
Or you don't have the right session.save_path or permissions to write there.
But if the problem persists, try this way:
<?php
session_start();
if (!$_SESSION['p']) {
$_SESSION['p'] = $_GET['p'];
$_SESSION['w'] = $_SERVER["HTTP_HOST"];
$_SESSION['u'] = $_GET['u'];
$_SESSION['k'] = $_GET['k'];
}
//code comes here
?>
In my opinion, this is the way things should be done.
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);