Forum Moderators: coopster

Message Too Old, No Replies

$_POST and $_SESSION back and forth

Found a solution but somehow doudbt about it!

         

henry0

3:27 pm on Jan 3, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I was fighting a bad case of session persistence
and found a solution but need to be reassured about is reliability
$dir=$_SESSION['dir']; //echo"dir.$dir.<br>";
if
($_SESSION['dir']=="")
{
$dir=$_POST['dir'];
$_SESSION['dir']=$dir;
$dir=$_SESSION['dir']; //echo"dir2.$dir.<br>";
}
else
{
$dir=$_SESSION['dir']; //echo"dir3.$dir.<br>";
}

<edit>
forgot stating that the problem was when the original file passed a POST value to other files and then going back to that original file from other locations.
</edit.

ergophobe

4:50 pm on Jan 4, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I think you have it backwards, plus several cases where with error reporting set to E_ALL you would get a notice. Normally in a situation like this, you want to give priority to the POST value, because that is how the value will get changed.

In other words, if the session value for 'dir' is "dir1" and on a page I use a form to post a new value of "dir2", your script will see that $_SESSION is not empty and will ignore the post value.

A few things in addition to tunring around the priority of evaluation.

1. you test for
$dir=$_SESSION['dir'];

This is redundant and will give an undefined variable notice if the session var is undefined.

2. You test for
if ($_SESSION['dir'] == "")
Again, this will give an undefined variable notice. Better to use isset() or empty()

I think this will do what you seek

$dir_prefix = "/path/to/dir/passed/by/post/or/session/with/trailing/slash/";

if (!empty($_POST['dir']) && is_dir($dir_prefix . $_POST['dir']))
{
$dir= (empty($_POST['dir'))? '' : $_POST['dir'];
$_SESSION['dir'] = $dir;
}
elseif (!empty($_SESSION['dir']) && is_dir($dir_prefix . $_SESSION['dir']))
{
$dir= (empty($_SESSION['dir'))? '' : $_SESSION['dir'];
}
else
{
$dir = '';
}

henry0

5:36 pm on Jan 4, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Sure thing thanks, that works
and makes more sense.

However as strange as it seems my wrong/OK solution did the trick?

I will rather use your way
actually a very good work around my problem.

it is a great solution but it brings me a headache thinking about: How built a script that at first uses POST to make its job (When initiated) that passes that POST as a SESSION but when coming back to that very first script can only rely on SESSION and not on POST for that values is passed around via SESSION and not POST?

ergophobe

8:29 pm on Jan 4, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Your original solution probably worked because you didn't test the case that would make it fail. If you have no dir specified and you POST one, and then never post again but use the session from there on out, your version works.

I'm sorry but I don't understand the last question.

henry0

7:04 pm on Jan 5, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Sorry, for some reason I did not get the "New post notification”

I know why my version works, I should have mentioned that if the file is accessed it is because a dir is submitted, so it’s impossible to access that file without a dir value passed. Another word submitting a dir calls for the file and pass the value
It actually is the only way to get to that file.

The second part of the previous post needs to be disregarded

Thanks for the help.