The header function is not working on online server? - php

hi i just dont understand why my code is not working. i am using yahoo server for my site.
This is my logout code.(which is successfully run on localhost) but when i upload this code online then its not work. plz help
<?php
//logout code
include("../Config.php");
if (!isset ($_SESSION['username']))
{
header( 'HTTP/1.1 301 Moved Permanently' );
header('Location: ../index.php');
if (!headers_sent())
{
header('Location: http://www.mysite.com/index.php');
exit;
}
}
else
{
$_SESSION = array();
session_destroy();
session_unset();
header( 'HTTP/1.1 301 Moved Permanently' );
header('Location: ../index.php');
if (!headers_sent())
{
header('Location: http://www.mysite.com/index.php');
exit;
}
}
?>
the config.php file includes session code (like start session)

You need to use the full URI in the header, and I recommend to use exit() right after the location header. There is no need for the 301 header for a simple log out.
And don't use the closing tag in php. If it is working on your system, it looks, there is some output (maybe just an empty line) in at least one of your php files (before the starting php tag, or after the closing php tag), and it seems that output buffering is enabled in your PHP, which work around this error, but disabled on the production server.
Try this:
<?php
// for debugging purposes only, don't use on production server (just for debugging)
error_reporting(E_ALL);
ini_set('display_errors', 1);
//logout code
include("../Config.php");
if (isset($_SESSION['username']))
session_destroy();
header('Location: http://www.mysite.com/index.php');
exit;

echo '<script type="text/javascript">
function delayer(){
window.location = "../index.php"
}
setTimeout("delayer()", 1000);
</script>';
You could put this instead of header

This will work
<script type="text/javascript">
window.location="http://www.newlocation.com";
</script>

Related

header("Location: ".$_SERVER['HTTP_REFERER']); stooped working when moved to a server

when running the code in xampp this statement works but when i moved the code to the production instance the code stops working.
ps: the way I'm evoking the php file is through a form submission.
if (!empty($_SERVER['HTTP_REFERER']))
header("Location: ".$_SERVER['HTTP_REFERER']);
else
print "<br> referpage:".$_SERVER['HTTP_REFERER'];
when printing header("Location: ".$_SERVER['HTTP_REFERER']); it is returning empty.
Try this
if(!empty($_SERVER['HTTP_REFERER'])){
$referer = $_SERVER['HTTP_REFERER'];
header("Location: $referer");
}
else
print "<br> referpage:".$_SERVER['HTTP_REFERER'];
so i did session and it worked.
1) you need session_start(); at the top of every file
2) on the main pages add $_SESSION['location'] = $_SERVER['REQUEST_URI'];
this stores the current page address as a string
3) call header("Location: " . $_SESSION['location']); on demand.

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

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.

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

header("Location: $value"); wont redirect... Why?

After login I want to send users back to $value,
$value is generated with my code, printed and looks ok.
It's a complete URL: http://example.com/page.php?id=6,
but it ignores the header("Location: ".$value); statement:
if($iniciando->iniciar()) {
if (isset($_SESSION['redirect'])) {
$he ="http://funcook.com" . $_SESSION['redirect'];
mostrar_notificacion($he);
header("Status: 301");
header("Location: " . $he, true, 301);
} else {
imprimir_sesion_iniciada();
}
} else {
imprimir_formulario_sesion();
}
header("Location x"); has to be called before any other output is sent to the browser. This includes any spaces outside the <?php and ?> markers.
Also, make sure you also don't have any print/echo statements for debug purposes.
I suspect you're using Chrome?
You should use a better redirect code for all browsers to follow it:
header("Status: 301");
header("Location: ".$URL, true, 301);

Categories