Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > PHP How-To
|
PHP How-To Post your "How do I do this with PHP?" questions here.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the PHP How-To section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 12th, 2005, 07:29 AM
Authorized User
 
Join Date: Nov 2005
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
Default Session variable problems

Hello All,

I have a page that passes a session variable to another page which is then used in an if/else statement. For some reason itsa giving me error messages, and I'm not sure whats up with it.

Heres the page that creates the variable

Code:
<?php

session_start(); // start session

$_SESSION['name'] = 'sport';

?>
and heres the page with the if else statment :

Quote:
quote:<?php

session_start();

$_SESSION['name'];

//$name = 'Jester';

if ( $name == 'sport' ) { ?>

<table>stuff in here</table>

<? } else { ?>

<table>stuff in here</table>

<? } ?>
and finally - here are the error messages that I am getting :

Quote:
quote:Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at c:\wamp\www\work\hebbingo\site\test.php:11) in c:\wamp\www\work\hebbingo\site\test.php on line 13

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at c:\wamp\www\work\hebbingo\site\test.php:11) in c:\wamp\www\work\hebbingo\site\test.php on line 13
As you've guessed, I'm pretty new to this so I have probably missed something basic - any ideas?

Cheers

Fogo

 
Old December 12th, 2005, 11:01 AM
richard.york's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts
Default

There can be no output before the opening <?php delimiter before a call to session_start() is made. session_start() alters the outgoing HTTP headers (sends a cookie), and that must be done before any output from the document is made. So look for whitespace or newlines, or anything before the opening <?php delimiter.

HTH!

Regards,
Rich

--
[http://www.smilingsouls.net]
Mail_IMAP: A PHP/C-Client/PEAR solution for webmail
Author: Beginning CSS: Cascading Style Sheets For Web Design
 
Old December 16th, 2005, 09:27 AM
Authorized User
 
Join Date: Nov 2005
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
Default

nice one - thanks

 
Old December 28th, 2005, 09:42 AM
Friend of Wrox
 
Join Date: Dec 2004
Posts: 154
Thanks: 0
Thanked 0 Times in 0 Posts
Default

page1.php:
<?php
session_start();
$_SESSION['name'] = "sport";
?>
<html><head></head><body>
<Form action='page2.php' method='POST'><input type="submit" value="Next Page" name="button1"></form>
</body></html>

page2.php:
<?php
session_start();
$name=$_SESSION['name'];
echo $name;
//$name = "Jester";
?>
<html><head></head><body>
<?php
if ( $name == "sport" ) { ?>
<table>stuff in here for sport</table>
<?php } else { ?>
<table>stuff in here for anything which is not sport</table>
<?php } ?>
</body></html>





Similar Threads
Thread Thread Starter Forum Replies Last Post
session and variable... badboy1 ASP.NET 3.5 Basics 1 August 3rd, 2008 09:14 AM
Session Variable youyou_hym Dreamweaver (all versions) 4 January 20th, 2005 11:48 AM
Session variable mrideout BOOK: Beginning ASP.NET 1.0 1 August 12th, 2004 07:01 PM
Session Variable mcdowell BOOK: Beginning ASP 3.0 5 June 20th, 2004 05:47 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.