Solved

Oscommerce- Why do custom session vars get destroyed.

Posted on 2009-07-13
12
1,206 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
  • 9
  • 3
12 Comments
 
LVL 108

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
 

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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 

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 108

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 108

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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Consider the following scenario: You are working on a website and make something great - something that lets the server work with information submitted by your users. This could be anything, from a simple guestbook to a e-Money solution. But what…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.

758 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now