Jump to content

[SOLVED] PHP Session Variables


musclehead

Recommended Posts

I have a bizzare session problem that I can't seem to figure out. I've used sessions a great deal in the past, and this is absolutely baffling me!

 

I'm doing some very simple checks against a session variable to either alert the user or not.

 

For some weird reason, when I refresh the page, the session variable is no longer set. I can't figure out why - it's simple enough. If the variable in the script itself is greater than the session variable (or if the session variable is not set at all), set the session variable and move on. On the reload of the script, if the original alert value has not incremented, nothing should happen. If it HAS gotten bigger, it should reset the session variable with the new value.

 

<?
session_start();
$alert = 0;

while (...some condition...){ $alert += 1; }

if (($alert > $_SESSION['alert']) || !isset($_SESSION['alert'])){
  echo "<script>alert('something');</script>";
  $_SESSION['alert'] = $alert;
}
?>

 

What am I missing?!?!?!

Link to comment
https://forums.phpfreaks.com/topic/133527-solved-php-session-variables/
Share on other sites

<?php
session_start();

// Check/set variables.
if (isset($_SESSION['alert'])) {
    $alert = $_SESSION['alert'];
}else {
    $alert = 0;
    $_SESSION['alert'] = 0;
}

$x=5;
while ($alert < $x){ $alert++; } // should set $alert equaled to after the while loop

// since we put isset up top remove it from here as the session variable should be set now.
if (($alert > $_SESSION['alert']) ){
  $_SESSION['alert'] = ($alert + 1); // add 1 to the alert
  echo "<script>alert('" . $_SESSION['alert'] . "');</script>";
}
?>

 

Give that a try and see what comes of it.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.