$_SESSION and include()

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
php_wiz_kid
Forum Contributor
Posts: 181
Joined: Tue Jun 24, 2003 7:33 pm

$_SESSION and include()

Post by php_wiz_kid »

OK, problem:

I created a user registration form working. I decided to modify it for security reasons. What I did was have one page called profile.php include the appropriate files according to the URL. The problem is that when I include the files I can't send anything from profile.php to the included page. I can't send variables, $_SESSION variables, nothing! So here's my code:

profile.php

Code: Select all

<?php
require($_SERVER['DOCUMENT_ROOT'].'/.../functions.php');
$doc_root = DocRoot('http://www.rockpromo.com');
session_start();
if($_GET['mode'] == 'register') {
	if($_GET['submit'] == 'true') {
		$_SESSION['sub'] = true;
	}
	include($doc_root['local']."/.../register.php");
} else {
	header("Location: ".$doc_root['local']."/index.php");
}
?>
register.php

Code: Select all

<?php
session_start();
echo $_SESSION['sub'];
?>
So in conclusion register.php will not print anything on the page when profile.php?mode=register&submit=true
User avatar
DuFF
Forum Contributor
Posts: 495
Joined: Tue Jun 24, 2003 7:49 pm
Location: USA

Post by DuFF »

Not sure on this, but it may be because you are setting it to true. Try something like:

Code: Select all

<?php
session_start(); 
if($_SESSION['sub'])
{
echo "It's Set";
}
else
{
echo "It's Not Set";
}
?>
php_wiz_kid
Forum Contributor
Posts: 181
Joined: Tue Jun 24, 2003 7:33 pm

Post by php_wiz_kid »

I get is not set with profile.php?mode=register&submit=true
User avatar
DuFF
Forum Contributor
Posts: 495
Joined: Tue Jun 24, 2003 7:49 pm
Location: USA

Post by DuFF »

Oh, sorry, last time I didn't have enough time to look over your code. The problem may be that you are including register.php, instead you should just use a header to redirect the user right to it. I know with cookies that you cannot use the cookie values until the script that created them has ended. So just make sure that this script ends and sends the user to the other page.
php_wiz_kid
Forum Contributor
Posts: 181
Joined: Tue Jun 24, 2003 7:33 pm

Post by php_wiz_kid »

Yeah, that's what I figured was happening. I included it so that the URL would stay profile.php, and I wouldn't have to switch around files into different, less sensitive folders. Thanks for the help. I'll come back here tomorrow if I have any more trouble.
Post Reply