0

I have a question regarding a session variable.

I have session variable which needs to start at a default variable. Then I need to be able to pass a new one through $_GET and keep that updated. So that even if the user reloads the page, it does not go back to the default value. How might I go about doing this? Thanks!

1
  • 1
    Just program each step of the algorithm you've described and ask a specific question when you'll have it. Commented Mar 5, 2011 at 3:36

4 Answers 4

2

With this snippet you'll have session variable assigning once:

if (!isset($_SESSION['magic'])) {
    $_SESSION['magic'] = isset($_GET['magic']) ? $_GET['magic'] : 1;
}
Sign up to request clarification or add additional context in comments.

1 Comment

would I just add: else $_SESSION['magic'] = 1; (or whatever I want my default to be?) because unfortunately that is breaking it ha
0

Seems like something along these lines might work:

if(!isset($_SESSION['my_parm']))
{
  $_SESSION['my_parm'] = 'DEFAULT';
}

if(isset($_GET['my_parm']))
{
  $_SESSION['my_parm']=$my_parm;
}

1 Comment

Unfortunately this is not working. After I change pages on my pagination, it does not stay at the amount of pages I want to show (my mysql LIMIT) which is the get variable I am trying to pass
0

Use

session_start();
$_SESSION

below is the link for session manual

http://www.php.net/manual/en/reserved.variables.session.php

2 Comments

Right, but I need to be able to update it to a $_GET variable, and then keep that even if the page is reloaded, and not go back to the starting default value
@rick , you can do it as per zerkms answer
0

If I understood well, this would be an outline of your method:

<?php
session_start();
if (isset($_GET['my_variable'])) {
    $_SESSION['my_variable'] = $_GET['my_variable']; // force new value
}
if (!isset($_SESSION['my_variable'])) {
    $_SESSION['my_variable'] = $default_value; // initialize
}
update_value($_SESSION['my_variable']);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.