Solved

Oscommerce- Why do custom session vars get destroyed.

Posted on 2009-07-13
12
1,217 Views
Last Modified: 2012-06-27
Hi

I'm developing an app using osc as the base. The problem is that my new session variables seem to get destroyed. I thought about using the tep_session functions but I can't work out how to assign and retrive a value.

Does anybody know why the session vars get destroyed and if not how you use the basic  tep_session functions.

Kind Regards,

Adrian Smith
0
Comment
Question by:lwfuk
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 9
  • 3
12 Comments
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 24841218
PHP Sessions, by default, may not be set in such a way that PHP can find them across sub-directories or sub-domains - there is a special was you need to set the session cookie to get this to work reliably.

Have you verified that the sessions are working right on the server?
0
 

Author Comment

by:lwfuk
ID: 24841655
Hi Ray

This works.
<?php
 
#Page1
require('includes/application_top.php');
if (!tep_session_is_registered('fig')) tep_session_register('fig');
$fig = "Charlotte";
?>
 
<?php
#Page2
require('includes/application_top.php');
echo $fig;
?>

Open in new window

0
 

Author Comment

by:lwfuk
ID: 24841669
Actually ignore the above. I'll put together a better demo.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:lwfuk
ID: 24841753
This Works.
<?php
 
//test.php
 
//Include the standard head
require('includes/application_top.php');
 
//Define the session var
if (!tep_session_is_registered('captcha')){
	  tep_session_register('captcha');
	  $captcha = rand()%9 . rand()%9 . rand()%9 . rand()%9 . rand()%9;
}
 
//Print an image
<?php echo '<img src="captcha.php" width="50" height="11"/>'  ?>
 
?>
 
 
<?php
 
//captcha.php
 
//Include the standard head
require('includes/application_top.php');
 
//Create an image 
$im = imagecreatetruecolor(50, 11);
$bgColor = imagecolorallocate($im, 0xFF, 0xFF, 0xFF);
$txt = imagecolorallocate($im, 0x00, 0x76, 0xC1);
imagefilledrectangle($im, 0, 0, 50, 20, $bgColor);
$font_file = 'truetype/arial.ttf';
 
//Add some text
imagefttext($im, 11, 0, 6, 11, $txt, $font_file, $captcha);
 
header('Content-Type: image/png');
imagepng($im);
imagedestroy($im);
 
?> 

Open in new window

0
 

Author Comment

by:lwfuk
ID: 24841758
This does not work.

<?php

//test.php

//Include the standard head
require('includes/application_top.php');

$_SESSION['captcha'] = rand()%9 . rand()%9 . rand()%9 . rand()%9 . rand()%9;

//Print an image
<?php echo '<img src="captcha.php" width="50" height="11"/>'  ?>

?>


<?php

//captcha.php

//Include the standard head
require('includes/application_top.php');

//Create an image
$im = imagecreatetruecolor(50, 11);
$bgColor = imagecolorallocate($im, 0xFF, 0xFF, 0xFF);
$txt = imagecolorallocate($im, 0x00, 0x76, 0xC1);
imagefilledrectangle($im, 0, 0, 50, 20, $bgColor);
$font_file = 'truetype/arial.ttf';

//Add some text
imagefttext($im, 11, 0, 6, 11, $txt, $font_file, $_SESSION['captcha']);

header('Content-Type: image/png');
imagepng($im);
imagedestroy($im);

?>
0
 

Author Comment

by:lwfuk
ID: 24841775
This does work.
<?php
 
//test.php
 
//Include the standard head
//require('includes/application_top.php');
 
$_SESSION['captcha'] = rand()%9 . rand()%9 . rand()%9 . rand()%9 . rand()%9;
 
//Print an image
<?php echo '<img src="captcha.php" width="50" height="11"/>'  ?>
 
?>
 
 
<?php
 
//captcha.php
 
//Include the standard head
//require('includes/application_top.php');
 
//Create an image 
$im = imagecreatetruecolor(50, 11);
$bgColor = imagecolorallocate($im, 0xFF, 0xFF, 0xFF);
$txt = imagecolorallocate($im, 0x00, 0x76, 0xC1);
imagefilledrectangle($im, 0, 0, 50, 20, $bgColor);
$font_file = 'truetype/arial.ttf';
 
//Add some text
imagefttext($im, 11, 0, 6, 11, $txt, $font_file, $_SESSION['captcha']);
 
header('Content-Type: image/png');
imagepng($im);
imagedestroy($im);
 
?> 

Open in new window

0
 

Author Comment

by:lwfuk
ID: 24841806
In the three examples above I use.

1. The osc way to set a session var
2. I use the standard PHP way with the "require('includes/application_top.php');"
3. I use the standard PHP way without the "require('includes/application_top.php');"

The middle way should work but the osc header seems to destroy the session. I've checked the code and I can't see why. I wondered if anybody had similar experience and had fixed it.

Kind Regards,

Adrian Smith
0
 
LVL 110

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 24842072
Hmm.  Have you tried using var_dump($_SESSION) before and after the "require('includes/application_top.php');"

Might be a clue there.  Use echo "<pre>\n"; to make the var_dump more readable.
 
0
 

Author Comment

by:lwfuk
ID: 24842209
Hi Ray

I'll have a go tomorrow. I'm rushing to finish something.

Kind Regards,

Adrian Smith
0
 

Author Comment

by:lwfuk
ID: 24867793
I'm still looking into this.

I found the following code snippet (Q_24191944.html) which is as Ray says but is a little more prescriptive.

echo '<pre>';
var_dump($_SESSION);
echo '</pre>';

Open in new window

0
 

Author Closing Comment

by:lwfuk
ID: 31602866
Many thanks for your help Ray. I tried the var_dump($_SESSION) method and my private sessions are fine. The only problem I have is when I try to call a GD image and pass the session too it. Anyway. I'm not really bothered any more because the osCommerce method I used above works fine. If I need to find a solution in the future I'll add it to the end of this post.

Anybody finding this post and wanting a solution - use the method at the top that I say is the one that works.

Many Thanks,

Adrian Smith
London
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 24905161
Thanks for the points.  Your comments are helpful, too, in making this a good one for the PAQ.

best, ~Ray
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question