Page 1 of 2

session problem

Posted: Mon Aug 03, 2009 9:50 am
by vorius
I am trying to make a page where the user submits a form via POST and then based on the result of some sql I display a message on the page following a redirect (so if the person refreshes the page they dont get the annoying popup about resending form data).

the problem is I cant seem to figure out how to stick this message into the session and then redirecting them and displaying it properly.

it either gets lost or never goes away!

i.e.:

Code: Select all

 
if (isset($_POST["form_variable"]))
{
  // do my sql
  if (whatever)
  {
    $msg = "Everything ok";
  } else {
    $msg = "Not good";
  }
  $_SESSION['msg'] = $msg; 
  header("Location: samepage.php");
  exit;
}
 
then later:

Code: Select all

 
<? if (isset($_SESSION["msg"]))
       {
         echo '<p class="warning">'.$msg.'</p>';  
         unset($_SESSION["msg"]);
         $_SESSION["msg"] = null;
       }
    ?>
 
not working... it always shows the message even when refreshing page...

what is the proper way to do this?

Re: session problem

Posted: Mon Aug 03, 2009 10:03 am
by jackpf
It could be due to the fact that you're unset()ing it, and then assigning it a new value of null.

Try removing the line:

Code: Select all

$_SESSION["msg"] = null;
If that doesn't work, try running var_dump() on $_SESSION to see what it really is.

Re: session problem

Posted: Wed Aug 05, 2009 9:40 am
by marty pain
I had loads of these problems last week.

Code: Select all

if (isset($_POST["form_variable"]))
{
   // do my sql
   if (whatever)
   {
     $msg = "Everything ok";
   } else {
     $msg = "Not good";
   }
   $_SESSION['msg'] = $msg;
   [color=#FF0000]write_session_close();[/color]
   header("Location: samepage.php");
   exit();
 }
This should work. You need to add write_session_close() before the header to make sure your session is being saved. Search for it on php.net for a big disscussion as to why.

Let me know how it goes

Re: session problem

Posted: Thu Aug 06, 2009 8:11 am
by jackpf
Oooh yeah, marty pain, I had a glance at your "$_SESSION issue" thread a while ago....

Nice to see you sharing your knowledge :D


I didn't think about write_session_close()....

Re: session problem

Posted: Thu Aug 06, 2009 8:15 am
by Benjamin
:arrow: Moved to PHP - Code

Re: session problem

Posted: Thu Aug 06, 2009 8:20 am
by straightman
have you noticed that at a time you are enclosing your word in brackets in double quotes in your session variables instead of single?

fix that, and try again

Alvaro

==================================================================================================


vorius wrote:I am trying to make a page where the user submits a form via POST and then based on the result of some sql I display a message on the page following a redirect (so if the person refreshes the page they dont get the annoying popup about resending form data).

the problem is I cant seem to figure out how to stick this message into the session and then redirecting them and displaying it properly.

it either gets lost or never goes away!

i.e.:

Code: Select all

 
if (isset($_POST["form_variable"]))
{
  // do my sql
  if (whatever)
  {
    $msg = "Everything ok";
  } else {
    $msg = "Not good";
  }
  $_SESSION['msg'] = $msg; 
  header("Location: samepage.php");
  exit;
}
 
then later:

Code: Select all

 
<? if (isset($_SESSION["msg"]))
       {
         echo '<p class="warning">'.$msg.'</p>';  
         unset($_SESSION["msg"]);
         $_SESSION["msg"] = null;
       }
    ?>
 
not working... it always shows the message even when refreshing page...

what is the proper way to do this?

Re: session problem

Posted: Thu Aug 06, 2009 8:31 am
by jackpf
Single and double quotes make no difference.

Here's the difference between single and double quotes.

Re: session problem

Posted: Thu Aug 06, 2009 11:27 am
by marty pain
jackpf wrote:Oooh yeah, marty pain, I had a glance at your "$_SESSION issue" thread a while ago....

Nice to see you sharing your knowledge :D


I didn't think about write_session_close()....
No worries mate, will always help where I can.

Re: session problem

Posted: Fri Aug 07, 2009 6:13 am
by vorius
hey guys, thanks for your replies!

it looks like the function is actually called "session_write_close" not "write_session_close" :wink: but it's not helping me!

I really don't understand what's happening with my code and it's getting frustrating! :banghead: :cry:

basically i have one page that displays the results of a sql query (using MySQL 5). these results are always fetched every time the page loads - it is not cached in session. it also should not be cached in the browser because I added:

<META HTTP-EQUIV="Pragma" CONTENT="no-cache">
<META HTTP-EQUIV="Expires" CONTENT="-1">

then I have a button the user may click to delete these records which does this:

Code: Select all

 
if (isset($_POST['action'])) {
  if ($_POST['action'] == 'delete') {
    // perform SQL DELETE
    $_SESSION['msg'] = 'some message'; 
    session_write_close();
    header("Location: mypage.php");
  }
}
 
then in my html I do:

<? if (isset($_SESSION['msg'])) { ?>
<p class="info"><?= $_SESSION['msg'] ?></p>
<? unset($_SESSION['msg']);
<? } else { ?>
// display records
<? } ?>


but what happens is when I press the button to initiate this delete, the page refreshes with the same records displayed. It should have deleted these records, put that msg in session and then display the msg on first redirect (where it is then taken out of session).

there is no other code here that can confuse things, its very simple.

I can sit the refreshing the page several times and it continues to display the deleted records. I can confirm in mysql these records are in fact gone. finally after just sitting there refreshing the page several times it realizes the records are gone and then does not display anything.

it is so weird...

it's as if there is this long delay between sql and php or there is some other cache.

what could be going on?

Re: session problem

Posted: Fri Aug 07, 2009 6:29 am
by marty pain
vorius wrote: it looks like the function is actually called "session_write_close" not "write_session_close" :wink: but it's not helping me!
Whoops, sorry mate! I hope I didn't waste your time. Answered on the quick, I should of checked.

First up, try adding die(); or exit(); on the line after your header redirect. This will make sure the script closes correctly after the redirect happens.

If that makes no difference, then make sure you are closing the data base session at the end of your script (or before the redirect) and starting a new every time the page loads.

Re: session problem

Posted: Mon Aug 10, 2009 3:27 am
by vorius
hey, no worries!

thank you for your replies and help!

I tried the exit command after my script completes and I already to a mysql_close following all my sql calls.

I am convinced now it is a browser cache issue but I am not sure how to fix it.

If I use IE it seems to work flawlessly (stupid I didn't think of trying IE before). But when I use firefox it is caching everything and I have to press the CTRL + F5 to actually refresh the screen...

That means these meta tags in my head section:
<META HTTP-EQUIV="Pragma" CONTENT="no-cache">
<META HTTP-EQUIV="Expires" CONTENT="-1">

are ignored by Firefox (At least my version 3.0.13).

does anyone know what tags firefox will actually obey? or is there something i can do in php to force it?

I defintely see those meta tags even when I do a View Source on my code.

Re: session problem

Posted: Mon Aug 10, 2009 4:07 am
by marty pain
Could you post the whole script please? It will make it easier to try it here.

Re: session problem

Posted: Mon Aug 10, 2009 4:10 am
by turbolemon
To disable caching, you ought not to rely on meta tags. HTTP Headers are much better! Something like this should do the trick:
http://www.w3schools.com/php/func_http_header.asp wrote:

Code: Select all

header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Cache-Control: no-cache");
header("Pragma: no-cache");
If you have apache, access to your server config, and mod_expires installed, you can define the expiry rules based on mime-type:

Code: Select all

ExpiresActive on
ExpiresDefault "access plus 2 weeks"
ExpiresByType text/html "access plus 1 week"
ExpiresByType text/css  "access plus 1 week"
ExpiresByType text/plain "access plus 1 week"
ExpiresByType application/x-httpd-php "now"
ExpiresByType application/x-httpd-fastphp "now"
ExpiresByType application/x-httpd-eruby  "now"
ExpiresByType application/xml "now"
http://httpd.apache.org/docs/2.0/mod/mod_expires.html

Re: session problem

Posted: Mon Aug 10, 2009 11:01 am
by jackpf
This seems odd though...I've never had a caching problem using firefox. Why would it randomly target yourself? :?

Have you set something in firefox config to cache stuff for a certain period of time...?

Re: session problem

Posted: Mon Aug 10, 2009 11:30 am
by vorius
Yes, it is quite odd...

I know now it cannot be my php code or a server side issue

It is something on the client side which I cannot figure out... and it's only on firefox. I have not touched my firefox config at all, I dont even have any add-ons. It is just a completely vanilla updated version.

I always have to hit CTRL + F5 on firefox to see refreshed screens, otherwise I get cached data.