Log in

View Full Version : how does sessions work ?


Mickie D
01-23-2006, 03:49 PM
i was wondering about sessions and wondered if anyone was experianced with using this feature and maybe can help a little ?

Say i had a page that had a form and the form sent infomation to another page (say a small popup - not adverts but info) and i would only want that page to display the info if the page i made sent the request ?

hope that sounds right it was a little difficult to explain and i think sessions can do this ?

for example if someone else sent the request i would want it to display the you do not have permission template ?

thanks for any advice

sde
01-23-2006, 03:57 PM
you can set session variables like this:
<?
$_SESSION['canViewTemplate'] = true;
?>
Then in your popup, you could test for it like this:
<?
if ($_SESSION['canViewTemplate']) {
// ok
} else {
// not ok
}
?>
I know the PHP side of things.. not sure about displaying templates. I'm trying to figure this out myself in a post I made today.

Mickie D
01-23-2006, 05:55 PM
so if i done something like this from the showpost.php page

insert somewhere


$_SESSION['camefromshowpost'] = true;


and in my new page whatever.php

add


if ($_SESSION['camefromshowpost']) {
$whatever = "yes";
} else {
$whatever = "no";
}



thanks very much for the reply

sde
01-23-2006, 06:02 PM
well once that is set, it is set for the entire session. you aren't really testing where they came from, you are testing if they have visited the page once during this session.

if you truely want to make sure they came from a certain page, use $_SERVER['HTTP_REFERER'].

for example, all you need on the popup page is: <?
if ( strstr($_SERVER['HTTP_REREFER'], "mydomain.com/mypage.php") ) {
$whatever = "yes";
} else {
$whatever = "no";
}
?>
I use strstr to compare because the referer might be different if it's a page that has query variables in the url. i.e. http://mydomain.com/mypage.php?f=1&b=2

Mickie D
01-23-2006, 06:51 PM
hi sde thanks i already use that method above ;)

but its a problem because many softwares like norton and loads of others block http referer so they wont get the variable i have if they came from showpost

anyway thanks very much for the help it looks like im back to the drawing board

sde
01-23-2006, 07:44 PM
session does seems like the answer then. you will at least know the user has visited the required page at least once during the session before launching the template.

Mickie D
01-28-2006, 12:21 PM
sorry to bring this back up again but i had some ideas about sessions ;)

just to clarify can that session code you posted above be used between to different php pages (non vb to vbpages)

thanks very much would be great if i could trace between pages where the user has been ;)

Regards
Mickie D

sde
01-28-2006, 03:53 PM
maybe something like this would work:
<?
// check if the current page is in the session array
if ( !in_array($_SERVER['SCRIPT_NAME'], $_SESSION['visited_pages']) ) {
// if not, add it
$_SESSION['visited_pages'][] = $_SERVER['SCRIPT_NAME'];
}
?>
so then to check if a user has visited a certain page, you could do this:<?
if ( in_array("somepage.php", $_SESSION['visited_pages']) ) {
// user has visited somepage.php
} else {
// user has not visited somepage.php
?>
keep in mind that $_SERVER['SCRIPT_NAME'] does not include the query variables. if you want to count the same php script with different query variables as different pages, then you should use $_SERVER['REQUEST_URI'] instead.

for example, with SCRIPT_NAME, these pages would all be the same:
/mypage.php?a=1
/mypage.php?a=52

REQUEST_URI includes the query variable in the value.