Forum Moderators: coopster

Message Too Old, No Replies

Session Variables Not Registering With HTML

         

HoboTraveler

6:25 pm on Aug 21, 2006 (gmt 0)

10+ Year Member



Hi All,

In the following example, I have a main.php that calls an include file. Basically, the main.php contains html code before the include is called. I need the variable created in the main.php to be carried across other pages. Hence, I begin the session in the include.php and the session continues in the view.php.

What happens is, due to the HTML code above the PHP, the session variable does not get carried across to the other pages.

Is there any possible way to carry the $test variable across the other pages through sessions and not through POST? The file main.php cannot be modified. Include.php and view.php can be modified.

-- Contents of main.php --

This section above the PHP contains HTML code
<?php
// This file cannot be modified
$test = "This is a Test";
include('include.php');
?>

-- Contents of include.php --

<?php
// This file can be modified
session_start();
$_SESSION['test'] = $test;
echo "<p><a href='view.php'>Click to View,$_SESSION[test]</a></p>";
?>

-- Contents of view.php --

<?php
session_start();
echo $_SESSION['test'];
?>

the_nerd

8:53 pm on Aug 21, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Try and output $test in include.php. If it's empty the session is not your problem.

In this case have look at $GLOBALS['test'], maybe there's something inside.

session_start();
$_SESSION['test'] = $test;

nerd.

HoboTraveler

4:30 am on Aug 22, 2006 (gmt 0)

10+ Year Member



Hello,

I am able to view the $test output in the include.php file. The issue is that the variable does not get carried into view.php

Is there a reason why this is happening? If I remove the HTML code in the main.php file, the variable does get registered and displays correctly in view.php. However, I do not want to remove the HTML in the main.php

-- Contents of include.php --
<?php
// This file can be modified
session_start();
$_SESSION['test'] = $test;

echo "<br>$test<br>";

echo "<p><a href='view.php'>Click to View, $test</a></p>";
?>

-- end --

TIA

[edited by: HoboTraveler at 4:36 am (utc) on Aug. 22, 2006]

the_nerd

10:24 am on Aug 22, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



did you try and start the session in the main program?

HoboTraveler

10:48 am on Aug 22, 2006 (gmt 0)

10+ Year Member



Hello,

Starting a sesson in main.php and modifying main.php is not an option. I am thinking of passing the variable through GET since the session does not work.

Is it possible to hide the variable in GET?

example:
view.php?category=php

However, the user sees the link as view.php without the variable..

view.php

the_nerd

11:17 am on Aug 22, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If I remove the HTML code in the main.php file, the variable does get registered

as soon as the first Byte is output to the browser (i.e. your HTML-Stuff), some header stuff is sent - and that seems to be colliding with your session.

IF you can't start the session IN the main file, maybe you could do it BEFORE and calling the main file later? Just guessing.

I'm sure it's possible to handle somehow - and wouldn't resort to adding funny parameters. No, you can't hide them - at least without using mod.rewrite and this way adding unneeded complexity.

HoboTraveler

11:32 am on Aug 22, 2006 (gmt 0)

10+ Year Member



There is no way I can modify main.php. Calling main.php is also not an option..

I can use mod rewrite. However, is it possible to hide the view.php?category=php

How do I hide the variables and yet pass the variable?

TIA

coopster

5:41 pm on Aug 22, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member




Note: If you are using cookie-based sessions, you must call session_start() before anything is outputted to the browser.

This includes your HTML code in the main.php. The only way around this is to buffer the HTML or store it in a variable as opposed to printing it to output.

HoboTraveler

6:18 pm on Aug 22, 2006 (gmt 0)

10+ Year Member



Ok.. sessions is not going to work... If I use GET in the URL, is it possible to hide the variables?

Thing is, I do not want the URL containing the variables to be bookmarked.

Basicaly, view.php?category=php should work only when using the link through main.php. Hence, I need the?category=php to be hidden.. How do I implement this with mod-rewrite?

TIA