954,174 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

PHP SESSION help

Hi,

I currently have a 1 page site where i am trying to store a session variable and then call it into various other pages that are included when different links are chosen. I currently have the code passing the variable to the first included page but it does not pass to the 2nd included code.

Someone has suggested it is because the variable is initially posted from a form

<?php session_start(); 

$_SESSION['app_name'] = $_POST['app'];
$_SESSION['serv_name'] = $_POST['server'];

?>


And then brought into the page in question

<?php session_start(); 

$app_name = $_SESSION['app_name'] 
?>


I basically then just call $app_name to get the variable.

However..when i try and bring in another page, it doesnt recognise the session variable anymore, as if it has been reset but me clicking on another link. Someone has suggested that this is because the form is posting to itself and that the page must be refreshing without a value when the 2nd link is clicked

they have suggested something along the lines of the following but i really dont know where to start with this. Any help would be much appreciated.

if  
($_POST['app'] is not null) and  ($_POST['app'] == "") then
    $_SESSION['app_name'] = $_POST['app'] ;
endif
ianmcd78
Newbie Poster
3 posts since Jul 2010
Reputation Points: 10
Solved Threads: 0
 

with the if statement your session will only be set if the app is blank. i would try it with an isset

<?php
session_start();
if(isset($_POST['app'])){
   $_SESSION['app_name'] = $_POST['app'];
}
?>
ProfessorPC
Posting Whiz in Training
270 posts since Dec 2007
Reputation Points: 31
Solved Threads: 29
 

This now does not pass the variable to any of the pages even though the form was posted.

ianmcd78
Newbie Poster
3 posts since Jul 2010
Reputation Points: 10
Solved Threads: 0
 
<?php session_start();
if(isset($_POST['app']) && !empty($_POST['app'])){
   $_SESSION['app_name'] = $_POST['app'];
}
echo $_POST['app'];
?>


Clear your cookies and then try.

rajarajan07
Nearly a Posting Virtuoso
1,447 posts since May 2008
Reputation Points: 167
Solved Threads: 239
 

thanks for that mate, works a charm

ianmcd78
Newbie Poster
3 posts since Jul 2010
Reputation Points: 10
Solved Threads: 0
 

Mark it as solved, if it works.

rajarajan07
Nearly a Posting Virtuoso
1,447 posts since May 2008
Reputation Points: 167
Solved Threads: 239
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You