I am trying to set some session variables, but for some reason they are getting deleted (I think). The url is: www.munzcomputers.com/po.php I have it showing the variable at the top of the "categories" page if it is set.. The code to set the sessions is: session_start(); if (!$ses_company){ session_register('ses_company'); $company = $_POST['company']; $_SESSION['ses_company'] = $company; $ses_company = $_SESSION['ses_company']; } if ($ses_company){ echo '<br>' . $ses_company . '<br>'; } if (!$ses_invoice_num){ session_register('invoice_num'); $invoice_num = $_POST['invoice_num']; $_SESSION['ses_invoice_num'] = $invoice_num; $ses_invoice_num = $_SESSION['ses_invoice_num']; } if ($ses_invoice_num){ echo '<br>' . $ses_invoice_num . '<br>'; } if (!$ses_date_ordered){ session_register('ses_date_ordered'); $date_ordered_year = $_POST['year']; $date_ordered_day = $_POST['day']; $date_ordered_month = $_POST['month']; $date_ordered = $date_ordered_year . $date_ordered_month . $date_ordered_day; $_SESSION['ses_date_ordered'] = $date_ordered; } PHP: You can look at the page and see exactly what it's doing.. I will show more code if needed.
Hmmm... for starters, I'd get rid of the session_register calls. Doing that and then setting the $_SESSION array value directly are kind or a 'one or the other' type situation and the session_register way is essentially deprecated. I'd get rid of those calls and see what happens, to be perfectly honest.
Your code could be cleaned up to simply a series of if(!isset($_SESSION['var'])) $_SESSION['var'] = $_POST['var']; Code (markup): Your problem may have to do with scoping since you define variables inside IF statements which may not be accessible outside that IF statement.
if (!isset($_SESSION['ses_company'])){ $_SESSION['ses_company'] = $_POST['company']; } if (isset($_SESSION['ses_company'])){ echo $_SESSION['ses_company']; } PHP: This is what I have changed it to, and it does the exact same thing.. Should I pose my whole code on here?
What I don't understand is that when I set $_SESSION['company'], and then echo the same thing, it works as if it has been set. Then, when I click on a link and the page reloads, it doesn't echo it. But, if I close the tab in my browser and re open the page, it has it set.
<?php session_start(); if (!isset($_SESSION['ses_company'])){ $_SESSION['ses_company'] = $_POST['company']; } if (isset($_SESSION['ses_company'])){ echo $_SESSION['ses_company']; } require_once('db_con.php'); include('index.php'); $main_cat = $_GET['main_cat']; $sub_cat_1 = $_GET['sub_cat_1']; ?> <html> <?php //if subcategory is set, show descriptions if (isset($sub_cat_1)) { $desc_result = mysql_query("SELECT * FROM `categories` WHERE `main_cat`='$main_cat' AND `sub_cat_1`='$sub_cat_1' ORDER BY `desc` ASC"); ?> <table border="1"> <?php while ($desc_row = mysql_fetch_array($desc_result)) { ?> <tr> <td align="center"> <a href="http://munzcomputers.com/po.php?cat_no=<?php echo $desc_row['cat_no'] ?>"> <?php echo $desc_row['desc']; ?> </a> </td> </tr> <?php } ?> </table> <?php exit; } //if the main category is set, show sub categories if (isset($main_cat)) { $sub_cat_1_result = mysql_query("SELECT DISTINCT main_cat,sub_cat_1 FROM categories WHERE main_cat = '$main_cat' ORDER BY sub_cat_1 ASC"); ?> <table border="1"> <?php while ($sub_cat_1_row = mysql_fetch_array($sub_cat_1_result)) { ?> <tr> <td align="center"> <a href="http://munzcomputers.com/categories.php?main_cat=<?php echo $main_cat; ?>&sub_cat_1=<?php echo $sub_cat_1_row['sub_cat_1']; ?>"><?php echo $sub_cat_1_row['sub_cat_1']; ?> </a> </td> </tr> <?php } ?> </table> <?php exit; } ?> <table> <?php //if no main category is set, show main categories if (y == $categ) { $main_cat_result = mysql_query("SELECT DISTINCT main_cat FROM categories ORDER BY main_cat ASC"); while ($main_cat_row = mysql_fetch_array($main_cat_result)) { ?> <tr> <td align="center"> <a href="http://munzcomputers.com/categories.php?main_cat=<?php echo $main_cat_row['main_cat']; ?>"><?php echo $main_cat_row['main_cat']; ?> </a></td> </tr> <?php } exit; } ?> </table> PHP: Just thought i'd post this .. hopefully someone can spot a problem.
Links are GET requests. So anything in the link won't be in the POST variable. You should just use $_REQUEST which has all the GET POST and COOKIE variables.
I understand that the links are GET requests, but it should be, and is, setting the cookie the first time that it loads the categories page. Then when it reloads the page, it acts like it doesn't have the cookie set. But if I close the tab on the browser and bring the page back up, the cookie is set.
When a session is started, shouldn't you be able to see anything that is set as $_SESSION until the browser is closed by calling the respective session?