Jump to content

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.

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.