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

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

Related

PHP Header Function Not Working Well

I have one file "djakhiltalreja_video.php" and another file "mobile_djakhiltalreja_video.php".i just want to redirect to this link http://akhil.djmusicweb.com/mobile_djakhiltalreja_video.php , current page :- djakhiltalreja_video.php .
but redirected url is http://akhil.djmusicweb.com/mobile_mobile_djakhiltalreja_video.php .
why double occurence of mobile_ ???
<?php
$pagename = "mobile_".basename($_SERVER['PHP_SELF']);
header('Location: http://akhil.djmusicweb.com/'.$pagename);
exit();
?>
Note : Remove mobile prefix from your page-name. I think it's covered in to $_SERVER['PHP_SELF']
Please check below solution for your problem.
Solution :
$pagename = basename($_SERVER['PHP_SELF']);
$url = "http://akhil.djmusicweb.com/".$pagename;
if (!headers_sent()) {
header('Location: '.$url);
exit;
} else {
echo '<script type="text/javascript">';
echo 'window.location.href="'.$url.'";';
echo '</script>';
exit;
}
This simple code will do the trick for you. It will check if headers are not sent, then it will call the PHP’s header function to redirect. But if the headers are sent, it will use Javascript to redirect to the URL you want.

Check url for information with php

I am wanting to check the url of one of my pages if the user has landed correctly
for example if a user visits www.example.com/page.php?data=something
The user will beable to view the page
but if the user visits www.example.com/page.php he gets redirected
This is my current code
$checkurl = 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
if (strpos($check, "?data=")!==false){
}
else {
header("Location: index.php");;
}
I thought this would work and been at this for a while cant seem to see a problem but i am still learning...
You need to use $_GET
For example
if (!isset($_GET["data"])) {
header("Location: index.php");
}
PHP Manual $_GET
Have you tried
if(isset($_GET['data']))
{
}
else
{
header("Location: index.php");
}
This way you just check if there is a "data" on your URL
You can just use this since you are looking for a query string aka a $_GET request:
if ($_GET['data'] != 'something') {
header('Location: http://test.com');
exit();
}
Also, if you just want to check if they included ?data=:
if (!isset($_GET['data'])) {
header('Location: http://test.com');
exit();
}

why ignores the header() function the result of my method?

$result = $mail->send($recipient, $headers, $html);
if($result === 1)
{
$report= "1";
header("Location: objednavka.php?reaction=".$report);
//echo("Your message has been sent!");
}
else
{
$report= "2";
header("Location: objednavka.php?reaction=".$report);
//echo("Your message was not sent: " . $result);
}
if this mail function runs down the if statement decide that if it was successfull or not. If I use the echo() part it writes that Your message has been sent. But if I want to redirect the user to another page it doesnt work. Why? How can I call the header function successfully?
From PHP - header():
Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP.
You forgot to set a corresponding status code (header())
header("Location: objednavka.php?reaction=$report", true, 301);

PHP Redirect with Cookies

How do I redirect to a splash page once with cookies?
I'm setting the cookie on my splash.php page to this:
<?php
$expire = time()+60;
setcookie("no_splash", "1", $expire);
?>
On that page there's a link to my index.php with this:
<?php
if($_COOKIE['no_splash']== '1') {
header("Location: index.php");
echo "it works";
} else if($_COOKIE['no_splash']!= '1') {
header("Location: splash.php");
};
?>
I keep getting a redirect loop error but can't figure why.
You are redirecting to index.php from the index.php file, hence the loop.
Change your code to be simply
if($_COOKIE['no_splash'] != '1') {
header("Location: splash.php");
exit;
}
or indeed
if(!$_COOKIE['no_splash']) {
header("Location: splash.php");
exit;
}
which is the same thing.
$expire = time()+60;
header("Set-Cookie: no_splash=1; expires=$expire; path=/");
header("Location: index.php");
Have you tried simply:
<?php
if($_COOKIE['no_splash']== '1') {
echo "it works";
} else {
header("Location: splash.php");
};
?>
Maybe isset($_COOKIE['no_splash']) rather than `$_COOKIE['no_splash']== '1'?
Also, not sure it this is what you want, but you can set simply not set the expiration time (or set it to 0), and it will delete the cookie when the browser is closed, so if they keep it open, they won't have to go back to the splash.

The header function is not working on online server?

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>

Categories