Page 1 of 1

PHP session variable works on local machine, but not server?

Posted: Fri Jun 05, 2009 1:53 pm
by ballinonabudget
I'm experiencing a strange unexplainable issue using PHP sessions if anyone has the time please help me identify the problem.

I have a form tool where users can select various SELECT fields to create a custom RF cable assembly. Each time a user selects a SELECT field value, the value is stored in a SESSION variable via the GET method. Here is one of my SELECT fields.

Code: Select all

 
    <select class ="cabletype" name="cabletype" onChange="MM_jumpMenu('parent',this,0)">
    <OPTION value="">----------</OPTION>
    <?php
    if (($series1=="IPX") || $series2=="IPX"){
       while($cable= mysql_fetch_array($ipx_cables_result)) {
    if ($cable['assembly']  == $cabletype) {
    ?>
    <OPTION value="rf-cable-assemblies-test.php?cable=<?php echo $cable['assembly'];?>" SELECTED><?php echo $cable['cable_name'];?></OPTION>
    <?php
    } else { 
    ?>
    <OPTION value="rf-cable-assemblies-test.php?cable=<?php echo $cable['assembly'];?>"><?php echo $cable['cable_name'];?></OPTION>
    <?php
   }
}
    } else {
    ?>
    <?php 
   while($cable= mysql_fetch_array($cable_result)) {
    if ($cable['assembly']  == $cabletype) {
    ?>
    <OPTION value="rf-cable-assemblies-test.php?cable=<?php echo $cable['assembly'];?>" SELECTED><?php echo $cable['cable_name'];?></OPTION>
    <?php
    } else { 
    ?>
    <OPTION value="rf-cable-assemblies-test.php?cable=<?php echo $cable['assembly'];?>"><?php echo $cable['cable_name'];?></OPTION>
    <?php
       }
    }
}
?>
 
Here is where I catch the value of GET and store in a SESSION

Code: Select all

 
if((isset($_GET)) && !empty($_GET['cable'])) {
    $_SESSION['cable']=$_GET['cable'];
    $selected11=1;
} else{
    $selected11=0;
}
 
//Setting the SESSION to a variable
$cabletype = $_SESSION['cable'];
 
ON my localhost testing server, this was working great! When I set the value of the other SELECT fields, the SESSION variables remained constant. The problem i noticed when I uploaded my script to the live server. For some reason the $_SESSION['cable'] variable would be erased when I set another SELECT field. I haven't changed any of the script from my local to live server. And the strangest thing is it is only this SESSION['cable'] variable I am having problems with even though all the SELECT field/SESSION variables are scripted the same way.

What would cause this particular SESSION variable to work on my local machine and not on the live server, when the other SESSIONS are working fine? :banghead:

Re: PHP session variable works on local machine, but not server?

Posted: Fri Jun 05, 2009 1:57 pm
by Darhazer
Where the session code is located? Before any output of after it? It is possible that on your local machine, the output buffer is on by default, and on the server is off. Also, please enable error reporting on the server and give us any warnings it's displaying

Re: PHP session variable works on local machine, but not server?

Posted: Fri Jun 05, 2009 2:24 pm
by ballinonabudget
The session code is down the page a bit in the body. I have this code a the top of the page and bottom.

Code: Select all

 
<?php session_start();?>
<?php ob_start()?>
 
<?php ob_end_flush()?>
 
Do you mean PHP error reporting?
I put this code <?php error_reporting(E_ALL);?> on the page but no errors.

Re: PHP session variable works on local machine, but not server?

Posted: Fri Jun 05, 2009 2:59 pm
by Darhazer
ballinonabudget wrote:
Do you mean PHP error reporting?
I put this code <?php error_reporting(E_ALL);?> on the page but no errors.
Add ini_set('display_errors', 'on');

Re: PHP session variable works on local machine, but not server?

Posted: Fri Jun 05, 2009 3:58 pm
by ballinonabudget
Thank you. I added the code, but there are no php errors.

Re: PHP session variable works on local machine, but not server?

Posted: Fri Jun 05, 2009 4:01 pm
by Loki
I don't know if it would actually have anything to do with your code or not, but everything that worked with GET on my local machine wont work unless I change the method to POST or REQUEST on my distribution server.

Re: PHP session variable works on local machine, but not server?

Posted: Fri Jun 05, 2009 4:32 pm
by ballinonabudget
Even stranger. I'm keeping track of if the SESSION exists or not:

Code: Select all

 
if (isset($_SESSION['cable'])){
    $_SESSION['selected11']=1;
} else {
    $_SESSION['selected11']=0;
}
 
Here is my output when I print the values in an array:
Array ( [selected1] => 1 [selected5] => 1 [selected11] => 1 [selected2] => 1 [selected6] => 1 [selected3] => 1 [selected7] => 1 [selected4] => 1 [selected8] => 1 [selected9] => 1 [selected10] => 1 [field1ok] => true [field2ok] => true [cableinfook] => true [series1] => SA [exception1] => 0 [orientation1] => L [gender1] => M [mounting1] => none [series2] => SA [exception2] => 0 [cable] => [orientation2] => S [gender2] => RM [mounting2] => 2 [units] => in [length] => 12 )
However, when I echo out:

Code: Select all

$_SESSION['cable'];
I get nothing, even though it tells me the SESSION variable is set in the array above.

Re: PHP session variable works on local machine, but not server?

Posted: Fri Jun 05, 2009 4:50 pm
by Benjamin
ballinonabudget - Start using

Code: Select all

tags when posting code please.

Re: PHP session variable works on local machine, but not server?

Posted: Mon Jun 08, 2009 4:34 pm
by ballinonabudget
Well.. Finally got this to work. All I did was change all instances of my variable "cable" to "cable1" and it worked like a charm. Not sure what might've caused it to not work earlier. Maybe a PHP bug? :wink: