session problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

vorius
Forum Newbie
Posts: 8
Joined: Fri Jan 18, 2008 7:05 pm

session problem

Post 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?
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: session problem

Post 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.
marty pain
Forum Contributor
Posts: 105
Joined: Thu Jun 11, 2009 5:32 am
Location: Essex

Re: session problem

Post 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
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: session problem

Post 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()....
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: session problem

Post by Benjamin »

:arrow: Moved to PHP - Code
straightman
Forum Commoner
Posts: 48
Joined: Sun Apr 19, 2009 5:20 am

Re: session problem

Post 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?
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: session problem

Post by jackpf »

Single and double quotes make no difference.

Here's the difference between single and double quotes.
marty pain
Forum Contributor
Posts: 105
Joined: Thu Jun 11, 2009 5:32 am
Location: Essex

Re: session problem

Post 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.
vorius
Forum Newbie
Posts: 8
Joined: Fri Jan 18, 2008 7:05 pm

Re: session problem

Post 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?
marty pain
Forum Contributor
Posts: 105
Joined: Thu Jun 11, 2009 5:32 am
Location: Essex

Re: session problem

Post 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.
vorius
Forum Newbie
Posts: 8
Joined: Fri Jan 18, 2008 7:05 pm

Re: session problem

Post 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.
marty pain
Forum Contributor
Posts: 105
Joined: Thu Jun 11, 2009 5:32 am
Location: Essex

Re: session problem

Post by marty pain »

Could you post the whole script please? It will make it easier to try it here.
User avatar
turbolemon
Forum Commoner
Posts: 70
Joined: Tue Jul 14, 2009 6:45 am
Location: Preston, UK

Re: session problem

Post 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
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: session problem

Post 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...?
vorius
Forum Newbie
Posts: 8
Joined: Fri Jan 18, 2008 7:05 pm

Re: session problem

Post 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.
Post Reply