I have this peace of code of which I don't understand why it's not working as I expect.
<?php
session_start();
if($_SERVER['REQUEST_METHOD'] == "POST") {
$_SESSION['reg'] = "done";
header("Location: " . SELF, true, 302);
}
if((isset($_SESSION['reg']) AND ($_SESSION['reg'] == "done"))) {
unset($_SESSION['reg']);
echo "Done";
}else{
echo "Not done";
}
?>
After POST it redirects and echo's Not done but I would expect it would echo's Done. If I remove the line with unset, it works fine and echo's Done.
This isn't the behavior I would expect. What mistake am I making?
A header redirect does not stop execution of the script, so the unset is carried out immediately.
To fix exit after the redirect
<?php
session_start();
if($_SERVER['REQUEST_METHOD'] == "POST") {
$_SESSION['reg'] = "done";
header("Location: " . SELF, true, 302);
exit();//<-- add this
}
if((isset($_SESSION['reg']) AND ($_SESSION['reg'] == "done"))) {
unset($_SESSION['reg');
echo "Done";
}else{
echo "Not done";
}
?>
write this unset($_SESSION['reg']); instead of this unset($_SESSION['reg');
Related
I have page called account_settings.php and it's consist of change password, change profile pic, change user details (name, bio etc.). My question is how to write message with echo() after redirecting page with header().
Something like this:
if (true)
{
Do_Some_MySQL();
header("Location: account_settings.php");
echo "Success!";
}
else
{
echo "Error!";
}
Thank you for all replies. ;-)
You can't actually do something after sending a Location header - it is impossible.
Instead, you could use $_SESSION array value to perform your task. Like:
if (true)
{
Do_Some_MySQL();
$_SESSION['message'] = 'Error!';
header("Location: account_settings.php");
}
else
{
echo "Error!";
}
And then on your account_setting.php:
<?php echo $_SESSION['message'] ?>
This would be nice if the account_settings.php is not the same page as you currently are. Otherwise, you could use the following code:
if (true)
{
Do_Some_MySQL();
$error = 'Success!';
header("Location: account_settings.php");
}
else
{
$error = "Error!";
}
And on the same page:
<?php if($error) echo $error; ?>
Also don't forget to include session_start() on both pages if you didn't it yet.
I would use a SESSION variable:
on redirect-page:
<?php
#session_start();
if(true){
$_SESSION['success'] = 1;
header("Location: account-settings.php");
}
?>
and on account-settings.php:
<?php
#session_start();
if(isset($_SESSION['success'])){
echo "Success!";
unset($_SESSION['success']);
}
You cannot echo anything after you just redirected. The browser is already processing the request to redirect to another page, so it doesn't bother about displaying the message anymore. What you seem to be looking for is something called flash message. You can set a temporary message in the session and have it display on the new page. For example, in your account_settings.php page:
// Make sure you have an actual session
if (!session_id()) {
session_start();
}
if (true) {
Do_Some_MySQL();
$_SESSION['flashMessage'] = 'Success!';
header('Location: account_settings.php');
}
Then in your template file for account_settings, check if there is any flash message and display it accordingly (and unset it to avoid a loop):
if (isset($_SESSION['flashMessage'])) {
echo $_SESSION['flashMessage'];
unset($_SESSION['flashMessage']);
}
These people are correct...you can't send headers after a redirect. Although I think this would be a beneficial alternative. To send a GET request in your header and process it on the receiving page. They are suggesting to use $_SESSION vars, but you can use GET vars. Ex:
if (true)
{
//Do_Some_MySQL();
header("Location: account_settings.php?message=success");
//above has GET var message = Success
}
else
{
header("Location: account_settings.php?message=error");
}
On your account_settings.php page have this code:
if (isset($_GET['message'])) {
$message = $_GET['message'];
if ($message == "success") {
echo "Success";
} else {
echo "Error";
}
}
This removes the need of CONSTANT SESSION vars. and gives you plenty of flexibility.
header("Location: account_settings.php?message=No%20results%20found");
//%20 are URL spaces. I don't know if these are necessary.
If you need you can add more then one.
header("Location: account_settings.php?message=error&reason=No%20Results×tamp=" . Date());
then account_settings.php can be:
if (isset($_GET['message'])) {
$message = $_GET['message'];
$reason = $_GET['reason'];
$time = $_GET['timestamp'];
if ($message == "success") {
echo "Success";
} else {
echo "Error: <br/>";
echo "Reason: $reason";
}
}
But remember GET exposes your messages in the browsers URL. So DON'T send sensitive information unless you secure it. Hope this helps.
I tried to check if the session is still exist, if it doesnt, it will kick me out from the page.
session_start();
if (empty($_SESSION['locationed']))
{
header("Location: http://exit.php");
die();
}
The code above always kick me out of the page.
When I delete the code above and echo the session:
$locationed=$_SESSION['locationed'];
<?php echo $locationed; ?>
It echos my session of location.
What is wrong? Help please.
Here you go .
if (!isset($_SESSION['locationed'])) {
header("Location: http://exit.php");
die();
}
try isset rather than empty.
if (!isset($_SESSION))
{
header("Location: http://exit.php");
die();
}
try:
if (isset($_SESSION['locationed']))
Try those combinations,
if (!isset($_SESSION['locationed']) && empty($_SESSION['locationed'])) {
...
}
session_start();
$_SESSION['locationed']="1234";
echo $_SESSION['locationed']; //if echo something means getting something on that
if($_SESSION['locationed'] =='' && !isset($_SESSION['locationed']))
{
header("Location: http://exit.php");
die();
}
session_start();
if(!isset($_SESSION["locationed"]) || empty($_SESSION['locationed']))
{
header("Location: http://exit.php");
die();
}
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.
On my index page I have a link to my login.php page with this code:
<?php
if(isset($_SESSION['username'])) {
echo "<div id='logout'><a href='logout.php'>Logout (".$_SESSION['username'].")</a></div>";
} else {
echo "<div id='login'><a href='login.php'>Login (Regular)</a></div>";
}
?>
On the login.php page I have
<?php
include('check.php');
$ref = getenv('HTTP_REFERER');
if (isset($ref)) {
header("Location: " . $ref);
exit;
} else {
header("Location: index.php");
exit;
}
?>
check.php is the code for the login form and it checks the users level to make sure they can access the page. I was told that I need to add a check to see if the referral is login.php, otherwise it will go in an infinite loop and I am of course getting "This webpage has a redirect loop". However, I have no clue how to do this and I can't find any information on how to fix it. Anyone know a quick solution?
You should be able to just do
if (isset($_SERVER['HTTP_REFERER']) && end(explode('/',$_SERVER['HTTP_REFERER'])) != 'login.php') {
header("Location: " . $_SERVER['HTTP_REFERER']);
exit;
} else {
header("Location: index.php");
exit;
}
Note that this is a simplified code - you may need to be a bit smarter than that.
I have a problem using the php method session_start();
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
session_start();
echo "Sesstion Started";
}
This is at the very beginning auf the php-file. If I execute this on my webserver and the requested method is POST, the page stays empty and it seems as if nothing is executed after the session_start();
Does anyone have an idea why this happens?
Cheers,
Cheeesi
If it were me running into this issue I would simply do this;
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
session_start();
echo "Sesstion Started";
}
else
{
echo "Error : Session NOT Started";
}
?>
This would help with the "empty" output until you can track down the error reporting.