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

Recommended Answers

All 5 Replies

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'];
}
?>

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

Member Avatar for rajarajan2017
<?php session_start();
if(isset($_POST['app']) && !empty($_POST['app'])){
   $_SESSION['app_name'] = $_POST['app'];
}
echo $_POST['app'];
?>

Clear your cookies and then try.

thanks for that mate, works a charm

Member Avatar for rajarajan2017

Mark it as solved, if it works.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.