Simple session varisable problem

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
spacebiscuit
Forum Contributor
Posts: 390
Joined: Mon Mar 07, 2005 3:20 pm

Simple session varisable problem

Post by spacebiscuit »

Hi,

Can anyone help with this simple session variable problem I have. I am registering and assigning a session variable in page 1 as follows:

Code: Select all

<?php

session_start();
session_register("testing");

$HTTP_SESSION_VARS ["testing"] = $_POST["del_add1"];

?>
The session variable is assigned the value of a form post. If I printr the session variable it outputs fine.

Now the problem is that I cannot access the session variable in a popup window. The code in my popup window is as follows:

Code: Select all

<? 
session_start(); 

$testing_2=$HTTP_SESSION_VARS["testing"];

echo"$testing_2";

?>
I get no output whatsoever, any ideas. Many thnaks,

Rob.
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post by shiznatix »

hummm somewhat odd but in your popup window did you try to print_r the session stuff or was it just after you echoed $testing_2 out that it got lost?
spacebiscuit
Forum Contributor
Posts: 390
Joined: Mon Mar 07, 2005 3:20 pm

Post by spacebiscuit »

i tried print_r and echo. I think the problem has occured because my function which invokes the popup was in a seperate script accessed via 'require'. There seems to be issues when using session variables and this method.

Thanks for the help but I gave up on session variables as I found another method.

Rob.
User avatar
Skara
Forum Regular
Posts: 703
Joined: Sat Mar 12, 2005 7:13 pm
Location: US

Post by Skara »

Erhm.. how to put it.
Imagine everything required into one file. session_start() has to be at the top of that file. In other words..er..

Code: Select all

blah blah blah
require 'something.php'

Code: Select all

session_start();
blaehfiehoihfe
won't work. That's the only issue with $_SESSION not working with included files I can think of.
spacebiscuit
Forum Contributor
Posts: 390
Joined: Mon Mar 07, 2005 3:20 pm

Post by spacebiscuit »

So are you saying that file accessed via 'require' do not work with session variables?

Rob.
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

I think he's saying that session_start() has to be above the require in order to use $_SESSION vars on the required page.
User avatar
Skara
Forum Regular
Posts: 703
Joined: Sat Mar 12, 2005 7:13 pm
Location: US

Post by Skara »

Er.. actually, I'm saying it has to be at the top of the page (apparently).
php.net comment wrote:I found that this only worked for me if I put session_start() at the very top of the page before the html and header tags.

<?php
session_start();
?>

<html>
<head>
Post Reply