Results 1 to 5 of 5

Thread: Session variables set with user interaction?

  1. #1
    Join Date
    Aug 2005
    Location
    Other Side of My Monitor
    Posts
    3,494
    Thanks
    5
    Thanked 105 Times in 104 Posts
    Blog Entries
    1

    Default Session variables set with user interaction?

    I am feeling like a dork today. I can't seem to make my mind work. Anyway I have 3 pages. One sets the session variable to "yes" The second and 3rd check if session is set to yes or not.

    If it is not, you are given the chance to set it. I want to do this without going back to page one. So, I thought, okay, make a text box, have user enter a text. I have the code to check the text, the problem is I can either set the session when the text box is show (using include() ) or not.

    I can't figure out how to set it if text matches.

    I have tried the following:

    PHP Code:
    <?php
    session_start
    ();
    if (
    $_SESSION['varset'] != "yes") { include 'sessioncheck.php'; }
    else{
    ?>
    This is on page 2 and 3, if varset is not = "yes" then you are shown the sessioncheck.php which has the text box and submit button on it.

    PHP Code:
    <?php
    session_start
    ();
    $_SESSION['varset'] = "yes"
    ?>
    <?php
    if($_POST['user']) {
    if((
    $_POST['user'] == "text"));
    } else {
    ?>
    With this way, the session is set when sessioncheck.php is shown, so it doesn't matter if you enter "text" or "nothing" or what, you have the session. and the page 2 or 3 loads.

    PHP Code:
    <?php
    session_start
    ();
    ?>
    <?php
    if($_POST['user']) {
    if((
    $_POST['user'] == "text"));
    $_SESSION['varset'] = "yes";
    if((
    $_POST['user'] != "text"));
    $_SESSION['setvar'] = "no";
    header('Location: http://mysite.com/401.shtml');
    } else {
    ?>
    This obviously doesn't work, the session is started but the varset is not set to yes, and you go to the 401.shtml page (which implies the varset = "no" but I don't think it is..

    Any help on what I can do to set the session only if "text" is entered in the text box, and not before, and not if anything is entered in the text box?
    {CWoT - Riddle } {Freelance Copywriter} {Learn to Write}
    Follow Me on Twitter: @InkingHubris
    PHP Code:
    $result mysql_query("SELECT finger FROM hand WHERE id=3");
    echo 
    $result

  2. #2
    Join Date
    Sep 2006
    Location
    St. George, UT
    Posts
    2,769
    Thanks
    3
    Thanked 157 Times in 155 Posts

    Default

    Ok, I got very confused for some reason when reading your post, so I hope this kinda points you in the right direction. Try the following:

    Code:
    <?php
     session_start();
    
    if (isset($_POST['submit'])) {
      if ($_POST['text'] == 'yes') {
        $_SESSION['var'] = 'yes';
      }
      else {
        $_SESSION['var'] = 'no';
        header('Location: 401.shtml');
      }
    }
    
     else {
    ?>
     <form action="<?php echo $_SERVER['PHP_SELF'];?>" method="POST">
     Set Session (yes or no):  <input type="text" name="text">
     <input type="submit" name="submit" value="Go">
     </form>
    <?php
     }
    Hope this helps.
    "Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music." - Kristian Wilson, Nintendo, Inc, 1989
    TheUnlimitedHost | The Testing Site | Southern Utah Web Hosting and Design

  3. #3
    Join Date
    Aug 2005
    Location
    Other Side of My Monitor
    Posts
    3,494
    Thanks
    5
    Thanked 105 Times in 104 Posts
    Blog Entries
    1

    Default

    That is VERY close to what I need. It does work.. however, when everything is correct you will get a blank white page, the session is set but you have to then refresh the page to get the page to show...

    is there away around that?

    (Many thinks, this is really been buggin me!)
    {CWoT - Riddle } {Freelance Copywriter} {Learn to Write}
    Follow Me on Twitter: @InkingHubris
    PHP Code:
    $result mysql_query("SELECT finger FROM hand WHERE id=3");
    echo 
    $result

  4. #4
    Join Date
    Sep 2006
    Location
    St. George, UT
    Posts
    2,769
    Thanks
    3
    Thanked 157 Times in 155 Posts

    Default

    If you are using the code I posted above, then you will want to edit this line with the following edit:

    Code:
      if ($_POST['text'] == 'yes') {
        $_SESSION['var'] = 'yes';
        header('Location: redirect.html');
      }
    Hope this helps.
    "Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music." - Kristian Wilson, Nintendo, Inc, 1989
    TheUnlimitedHost | The Testing Site | Southern Utah Web Hosting and Design

  5. #5
    Join Date
    Aug 2005
    Location
    Other Side of My Monitor
    Posts
    3,494
    Thanks
    5
    Thanked 105 Times in 104 Posts
    Blog Entries
    1

    Default

    Will that work though? I need it to go back to page2 and page 3, depending on which you come from.

    I tried the $HTTP_REFERER but you still had to manually refresh to get the page2 or page3 to show...


    this is so frustrating.

    Edit:
    HA! Got it! I also thought I tried this before but I don't know. It must have been a long night for me.

    PHP Code:
    header('Location: '.$_SERVER['HTTP_REFERER']); 
    Now to test it on multiple browsers!
    Last edited by BLiZZaRD; 11-25-2007 at 10:35 PM.
    {CWoT - Riddle } {Freelance Copywriter} {Learn to Write}
    Follow Me on Twitter: @InkingHubris
    PHP Code:
    $result mysql_query("SELECT finger FROM hand WHERE id=3");
    echo 
    $result

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •