Jump to content

Recommended Posts

I need assistance badly - i am writing a shopping cart script.  basic page settup looks like this

 

require_once all files needed

load_application

  session_initialize

  check if logged in

  load user information

  load cart information

begin page logic

  blah blah

begin page display

end page display

clean up

 

all error handling is handled in the cart, user, and session classes - all redirects are used prior to any output other than log file entries.

My problem is one of confusion.

The following statement in my sessions class used to work a week ago, but now it does not (error_reporting(E_ALL))

 

else

{

  $this->Reset();  //resets session array

  header("Location:  timeoutpage.php");

}

 

so, instead of redirecting me, it just continues through my script (and dies, because this block executes if the user times out and the session variables hold encrypted user and cart id.  i don't know what is causing this - its funny, because my script actually executes a redirect further down the line when the app fails to load an empty user.

 

this all got more confusing when i tried troubleshooting:

as expected, placing any output prior to the redirect in question resulted in a failure

else

{

  $this->Reset();

  echo 'attempting redirect';

  header("Location: timeoutpage.php");  //we know this will fail but want to make sure code gets here

}

 

when the session times out, it is reset, echoes attempting redirect, then prints error cannot set headers - as expected

this is what baffles me - on a crazy moment, i wrote the following

else

{

  $this->Reset();

  header("Location: timeoutpage.php");

  echo 'failed to redirect';

}

 

this block of code worked as expected and redirected me to the timeout page.  say whhaaaaat?  i say again, the block with the echo statement after the header statement worked.

the block with no echo statement after the header statement skips the header statement.

i am 100% sure that this block of code is executing.

 

what is going on?

 

thanks,

b

Link to comment
https://forums.phpfreaks.com/topic/168890-solved-i-fly-my-white-flag-in-surrender/
Share on other sites

This just gets more and more confusing -

 

 

the following doesn't work

$this->Reset();
header('Location: timeoutpagegoeshere");

 

the following does work

$this->Reset();
ShutdownApplication();
header('Location: timeoutpagegoeshere");

here is the ShutdownApplication() function:

 

function ShutdownApplication()
{
global $db;
global $logfile;
global $endtime;
@mysql_close($db);
@fclose($logfile);
$endtime=microtime(true);
}

 

anyone have any explanations as to why running through this function would cause the headers() to work, while without this function this header() call is being skipped?

 

thanks,

b

 

well first of all, you can set a header when you already have output on the page. I'm assuming this is why you could not set the header in the following code block

 

else
{
  $this->Reset();
  echo 'attempting redirect';
  header("Location: timeoutpage.php");  //we know this will fail but want to make sure code gets here
}

 

check if you have any output before this else statement. if you do, you cannot use the header, because it cannot execute a header command when there has been output.

 

hope that helps!

Thanks mikesta,

 

I may have confused people with that code block - that was simply a test to prove to myself that the script was executing to that point.

I fully expected with that specific block of code that the header function would throw the output error - and it did.  which is why i am so confused, because the script is clearly executing to that point.

 

But by removing the echo statement in the test block and rerunning the script, the script skips right over the header statement.

 

I have verified multiple times there is no output before the header command.  Below is the relevant code with some non-relevant language removed for confidentiality.  Please note there is ZERO HTML output

in this script, only file output.

 


//the ->Initiate() function returns an array with user email and cartid
//if the session is timed out, it will redirect to timeoutpage
public function Initiate()
if( user is not timed out )
{
  do something
  return(array containing email and cartid)
}
else
{
  //reset the session variables
  $this->Reset();
  //log event in logfile
  LogEvent('session timed out - attempting redirect');
  //redirect to timeoutpage - this is the line that is not executing, but also not throwing an error
  header("Location: timeoutpage.php");
}


actual script

//create new session object - constructor simply calls session_start()
$session=new Session();

//this should return an array with email and cartid on success, or redirect on failure
$result=$session->Initiate();

//user load returns true on success, false on failure
$errorcheck = $user->Load($result['email']);

//if user load returns false, redirect to error page
if(!$errorcheck)
{
  LogEvent('error loading user, email: '.$result['email']);
  header("Location: unspecifiederror.php");
}

 

in simple terms, the page is constructed by calling all page logic first, before any actual html is output

all redirect statements occur in the logic.

when i execute the aforementioned code, and the user is timed out,

I get a log message stating 'session time out, attempting redirect'

the next log message states 'error loading user, email: '

and the page is redirected to the unspecifiederror page.

 

this indicates that the flow of things happening is:

1) user times out

2) initiate() function logs appropriate timeout event (attempting redirect)

3) ***the next line never executes???  no error???***

4) since the header is never executed, Initiate() returns nothing, so $result['email'] is blank (as evidenced in the second log message)

5) that leads to $user->Load returning false (because it cant load a blank user)

6) which consequently leads to the error loading user, email log event

7) and for some reason, the redirect at this point works?

 

it is clear from the logs that the script passes through the timeout code block and continues into the error codeblock without redirecting.

it is also clear there is no html being output before the call to the timeout header()

 

error_reporting is set to E_ALL

 

Any help is much appreciated.

 

Thanks,

b

the following doesn't work

$this->Reset();
header('Location: timeoutpagegoeshere");

 

the following does work

$this->Reset();
ShutdownApplication();
header('Location: timeoutpagegoeshere");

 

That is odd...

Try this test..

instead of ShutdownApplication() create a dummy function that does something like set a variable to 1

 

so you'd have

function dummY() {
   $i = 0;
}
$this->Reset();
dummY();
header('Location: timeoutpagegoeshere");

 

now if that does work then I really am confused...

but if you have the same...not working... problem...then I'd go through one at a time and figure out which statement in ShutdownApplication() is causing you trouble.

Hi Zanus,

 

Good idea - I tried this out.  Dummy function didn't work.  So I commented out each line of the ShutdownApplication() function, and it turns out the header redirect to the timeout page only works if mysql_close($db) is called.  If that statement is not called prior to the header function, not only does the redirect not work, but it also throws no error - just continues on through the script.  Is there documentation on this?  I've never heard of anything like this.

 

Thanks,

b

Hosting with godaddy.

testing on IE8 and IE7.

Can someone try this with there own mysql db?

 


<?php

$db=mysql_connect("yourdbaddress","yourlogin","yourpassword");

header('Location: http://www.google.com');

mysql_close($db);
header('Location: http://www.msnbc.com');


?>


 

when i run this, i end up at msnbc.com.  please confirm/refute?

 

thanks,

b

FYI:

 

Using the helpful information provided by numerous users, I used a combination of log files and headers_sent to complete the troubleshooting process.  Turns out that calling mysql_close before the header() redirect was not the solution for the reason I originally thought.  What happened is that a subsequent call to mysql_real_escape produced an error, which in turn sent the headers with the correct redirect information.

 

Thanks to everyone that helped with this issue.

Much appreciated,

b

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.