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

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ballinonabudget
Forum Newbie
Posts: 8
Joined: Wed May 27, 2009 6:32 pm

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

Post 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:
Last edited by Weirdan on Fri Jun 05, 2009 2:31 pm, edited 1 time in total.
Reason: added [code=php] tags
User avatar
Darhazer
DevNet Resident
Posts: 1011
Joined: Thu May 14, 2009 3:00 pm
Location: HellCity, Bulgaria

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

Post 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
ballinonabudget
Forum Newbie
Posts: 8
Joined: Wed May 27, 2009 6:32 pm

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

Post 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.
User avatar
Darhazer
DevNet Resident
Posts: 1011
Joined: Thu May 14, 2009 3:00 pm
Location: HellCity, Bulgaria

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

Post 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');
ballinonabudget
Forum Newbie
Posts: 8
Joined: Wed May 27, 2009 6:32 pm

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

Post by ballinonabudget »

Thank you. I added the code, but there are no php errors.
Loki
Forum Commoner
Posts: 25
Joined: Wed Jun 03, 2009 9:23 pm

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

Post 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.
ballinonabudget
Forum Newbie
Posts: 8
Joined: Wed May 27, 2009 6:32 pm

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

Post 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.
Last edited by Benjamin on Fri Jun 05, 2009 4:50 pm, edited 1 time in total.
Reason: Added [code=php] tags.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

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

Post by Benjamin »

ballinonabudget - Start using

Code: Select all

tags when posting code please.
ballinonabudget
Forum Newbie
Posts: 8
Joined: Wed May 27, 2009 6:32 pm

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

Post 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:
Post Reply