PHP Timed redirect not redirecting? - php

Have this code on my site to redirect users back to the homepage once logged out and destroys their session. Was working perfectly before when I was using my other hosting account to host the site, but now I've changed the host, it doesn't seem to work anymore ? It isn't actually doing anything. It does destroy session but doesn't redirect? The domain has remained the same and everything so I don't understand what is wrong here? Any ideas?
<?
session_start();
if(!isset($_REQUEST['logmeout'])){
echo "<strong><font color=green>Are you sure you want to logout?</font></strong><br />";
echo "<a href=logout.php?logmeout>Yes</a> | <a href=javascript:history.back()>No</a>";
}
else {
session_destroy();
if(!session_is_registered('first_name')){
echo "<center><font color=red><strong>You have now been logged out.</strong></font></center><br />";
echo "<center>You will be redirected in 3 second...</center><br />";
/* Redirect browser */
header('Refresh: 3;url=http://www.basecentre.co.uk/');
/* Make sure that code below does not get executed when we redirect. */
exit;
}
}
?>

TRY THIS
echo "<meta http-equiv='refresh' content='0;url=http://www.yoursite.com'>";
OR
use flush() before header call

Your previous hosting may have had automatic output buffering enabled.
To avoid "headers already sent" errors please change
echo "<center><font color=red><strong>You have now been logged out.</strong></font></center><br />";
echo "<center>You will be redirected in 3 second...</center><br />";
/* Redirect browser */
header('Refresh: 3;url=http://www.basecentre.co.uk/');
to
/* Redirect browser */
header('Refresh: 3;url=http://www.basecentre.co.uk/');
echo "<center><font color=red><strong>You have now been logged out.</strong></font></center><br />";
echo "<center>You will be redirected in 3 second...</center><br />";
And note that the header() function is occurring before the echo of any content.

You can't do header after any output. There's a setting in the php.ini to change this, but otherwise, it's better practice to send your headers before any output.
But, it looks like you're trying to give them a notification before they get redirected anywhere. To preserve this, just do it with javascript the same way you did on the other one..
echo "<center><font color=red><strong>You have now been logged out.</strong></font></center><br />";
echo "<center>You will be redirected in <span id="time">3<span> second...</center><br />";
//And then echo the redirect script.
echo <<<JAVASCRIPT
<script>
var count = 3;
var counter = setInterval(timer, 1000);
function timer() {
count = count - 1;
if (count <= 0) {
window.location.pathname = '/user/index';
}
document.getElementById("time").innerHTML = count;
}
window.onload = timer();
</script>
JAVASCRIPT;

Related

Php Header redirects, but doesnt echo after that

It redirects them back to the homepage but I want it to also display a box.
if($m->send()){
header('Location:http://blankwebsite.com/');
echo '<script>
alert("Your Quote Request has been submitted!");
</script>';
}
else{
echo $m->ErrorInfo;
}`
You could save the html in a session variable, display it after redirect and empty the session variable like a flash message at script start up.
if($m->send()){
$_SESSION['redirectMessage'] = base64_encode(utf8_encode('<script>alert("Your Quote Request has been submitted!");</script>'));
header('Location:http://blankwebsite.com/');
}
else{
echo $m->ErrorInfo;
}
On new page request or redirect:
if isset($_SESSION['redirectMessage']) {
echo htmlentities(base64_decode($_SESSION['redirectMessage']), ENT_QUOTES, 'utf-8');
$_SESSION['redirectMessage'] = null;
}
This solution uses sessions, so do make sure session_start() is called at the top of the script.

Getting Redirect Loop Error for the Header

I am getting a redirect Error for the following code. It's just that when the form is submitted and certain username is checked, it should redirect to a certain page.
<div>
<?php
global $user;
if (isset($user->name)) {
echo "You don't have permission to access the administrator with the current logged in user (".$user->name."). ". "<a href='./user/logout'>Log Out as (" . $user->name . ")</a>"."<br /><br />";
}
?>
Please refer to the credentials sent to you.</div>
<?php
if ($user->name == "a-particular-user") {
header( 'Location: http://url.to.direct' ) ;
}
?>
On the Redirect Page, following code exits
global $user;
$adminuser = false;
if (in_array('administrator', array_values($user->roles)))
$adminuser = true;
if ( !($adminuser) && ($user->name != "rockstar")) {
echo "<br /><br />";
echo "<h2> You need to be an Administrator to see this page.</h2>";
exit();
}
The problem is that the code is in the same page as the one you're redirecting to. It causes a loop.
You can solve this with a GET variable:
if ($user->name == "a-particular-user" && !isset($_GET['adminpage'])) {
header( 'Location: http://url.to.direct?adminpage=true' ) ;
}
Why would you ask a question about an error message you're getting, but not include the error message?
Does the error message say something about "headers already sent"?
You can't use header() to redirect after you've already generated content, even if it's only a space or a blank line. In this case, you have a <div> in the first line no matter what else happens. So, you won't be able to send a header() after that.
ref: How to fix "Headers already sent" error in PHP

Page Does Not Recognize SESSION

I have come along something i could not solve for so long.
i have created a script in php that unsets one single session variable, However the page stats the session Here is my code for the page :
<?php
session_start();
require_once("../header.php");
if($_SESSION['user']) {
unset($_SESSION['user']);
echo "you succesfully logged out.";
header("Refresh:5; url=http://www.webmasteroutlet.com");
} else {
echo "you are already NOT LOGGED IN right now.";
}
require_once("../footer.php");
?>
That is the whole code on this page. and it always prints out "you are already NOT LOGGED IN right now." $_SESSION['user'] is assigned true in login.php page and i have session_start(); at the very beginning of the page right after the <?php opening.
The session variable is recognized at all other files with php extension and that is the only single file that it is not working on. I also tried
<?php
session_start();
echo $_SESSION['user'];
?>
and it does not print anything. It simply skips that line and does nothing. What am i doing wrong ?
Thank You very much for your help.
this is the header.php code
<?php
session_start();
require("config.php"); // that only contains connection to the database and it is successful.
if(isset($_SESSION['user'])==1){
echo "<div id=\"topnav\" class=\"topnav\"><span>".$_SESSION['username']."</span> <span>LOGOUT</span></div>";
}
else if ($_SESSION['admin']) {
echo "<div id=\"topnav\" class=\"topnav\">"."<span>".$_SESSION['adminusername']."</span> ";
echo "<span>LOGOUT</span></div>";
}
else if ( !isset($_SESSION['user'])) {
require ($_SERVER['DOCUMENT_ROOT']."/users/login.php");
}
require("search.php");
?>
i think you need the if is set and make sure you pass the sessions data to this page it looks like your unsetting this
Try this:
<?php
session_start();
require_once("../header.php");
if(isset($_SESSION['user'])) {
echo "User.".$_SESSION['user']." you are being logged out";
unset($_SESSION['user']);
header("Refresh:5; url=http://www.webmasteroutlet.com");
} else {
echo "You are not logged or var SESSION doesnt exist";
}
require_once("../footer.php");
?>
If still doesnt work, try deleting the require_once lines(for debug).
Justin, I think you're not setting the $_SESSION['user']. That'd be the reason why you're getting NULL when you vardump.
One other possibility, although I'm limited to the scripts you provided, would be that you made it possible for a person to login through $_SESSION['admin'] as well as $_SESSION['user']. If this is the case you'd have to change the script to:
if(isset($_SESSION['user'])) {
unset($_SESSION['user']);
echo "user succesfully logged out.";
}elseif(isset($_SESSION['admin'])){
unset($_SESSION['admin']);
echo "admin succesfully logged out.";
}else{
echo "you are already NOT LOGGED IN right now.";
}

setcookie doesn't work in the correct way

I have
<?php ob_start(); ?>
<?php
$rng2 = random_string('alnum', 24);
setcookie("rng2", md5($rng2), time()+7200, '/');
?>
as my first 5 lines of my page. I do have <?php ob_end_flush(); ?> at the end of my page.
I am also checking the value of the cookie after page reload with the lines
print_r($_COOKIE);
echo "<br /><br />".$_COOKIE['rng2']."/cookie[rng2]<br />";
echo $_POST['f']."/post[f]<br />";
echo md5($_POST['f'])."/md5(post[f])<br />";
if($_COOKIE['rng2'] != md5($_POST['f'])){
$err .= "There was an error submitting the form.<br />";
}
the cookie ['rng2'] does not show up in my print_r() and the variable $_COOKIE['rng2'] displays no space between the two "/"'s, the $_POST['f'] comes up correct.
I can't seem to get this to work. I can't find anything online about having it matter if the page the cookie is set on is an include but I figured I'd mention that it is.
Any insight would be helpful. Thank you.

javascript redirecting to main window doesn't take into account http option (&)

i'm triying to use javascript redirect to main window after log in succeed in an iframe.
this is my code :
if ($_GET['redirect']!='') {
$redirect=$_GET['redirect'];
$smart->assign('redirect',$redirect);
}
$redirect=$_GET['redirect'];
echo $redirect;
if(isset ($_SESSION['user'])&&$_SESSION['user']!='') {
$user->email=$_SESSION['user'];
$user->addCorporate();
$user->signIn();
$user->loadSession();
echo("<script language=\"javascript\" type=\"text/javascript\">");
echo "document.write('redirecting...');";
if ($redirect!='') {
echo 'self.parent.location = "'.$redirect.'"';
} else
echo 'self.parent.location = "index.php"';
//echo $redirect;
// redirect($redirect);
echo "</script>";
}
the echo $redirect displays http://xxxxxxxx/play.php?action=play&id=d59541b89828da34e9a8345a1bdafe2b
but the redirection is made to http://xxxxxxxx/play.php? (without the php option)
This sounds pretty mysterious.
Here's how I'd proceed: Turn off JavaScript in your browser and examine your created JavaScript. Then, at least, you know whether maybe for some bizarre reason the wrong URL is print out after all, or whether the problem's in the redirection part.
If you change the line:
echo 'self.parent.location = "'.$redirect.'"';
to this:
echo 'alert("'.$redirect.'")';
What happens?

Categories