Log in

View Full Version : Session variables set with user interaction?



BLiZZaRD
11-25-2007, 07:07 PM
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
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
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
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?

thetestingsite
11-25-2007, 07:45 PM
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:



<?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.

BLiZZaRD
11-25-2007, 08:21 PM
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!)

thetestingsite
11-25-2007, 08:42 PM
If you are using the code I posted above, then you will want to edit this line with the following edit:



if ($_POST['text'] == 'yes') {
$_SESSION['var'] = 'yes';
header('Location: redirect.html');
}


Hope this helps.

BLiZZaRD
11-25-2007, 08:58 PM
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.


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



header('Location: '.$_SERVER['HTTP_REFERER']);

Now to test it on multiple browsers!