Link to home
Start Free TrialLog in
Avatar of lwfuk
lwfuk

asked on

Oscommerce- Why do custom session vars get destroyed.

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
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

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?
Avatar of lwfuk
lwfuk

ASKER

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

Avatar of lwfuk

ASKER

Actually ignore the above. I'll put together a better demo.
Avatar of lwfuk

ASKER

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

Avatar of lwfuk

ASKER

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);

?>
Avatar of lwfuk

ASKER

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

Avatar of lwfuk

ASKER

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
ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of lwfuk

ASKER

Hi Ray

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

Kind Regards,

Adrian Smith
Avatar of lwfuk

ASKER

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

Avatar of lwfuk

ASKER

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
Thanks for the points.  Your comments are helpful, too, in making this a good one for the PAQ.

best, ~Ray