Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > Beginning PHP
|
Beginning PHP Beginning-level PHP discussions. More advanced coders should post to the Pro PHP forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Beginning PHP 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 4th, 2003, 05:41 AM
Friend of Wrox
 
Join Date: Sep 2003
Posts: 363
Thanks: 0
Thanked 1 Time in 1 Post
Default Session variables problem

Hello friends,

  file1.php
session_register("var1");
echo "session created";
$HTTP_SESSION_VARS["var1"]=0;


file2.php
echo $HTTP_SESSION_VARS["var1"];
$HTTP_SESSION_VARS["var1"]=$HTTP_SESSION_VARS["var1"]+1;

whats wrong with these two files. First file didnt produce any error. The second file didnt display any value.

I used $_session["var1"] instead of $HTTP_SESSION_VARS["var1"]

but no display.

pls, clearify my problem. how to manage sessions?

thanx in advance

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

Don't forget that variable names are case sensitive. $_session is not the same as $_SESSION. The latter is the correct way to reference the session superglobal. Don't use the long names... $HTTP_*_VARS... these aren't going to exist in PHP 5. If you are using PHP prior to version 4.2.0.. write a script that transfers the old long variable names to the short ones. If you need us to elaborate more on that just ask.

<?php

// file1.php

session_start();

$_SESSION["var1"] = (int) 0;

echo "session created";


?>
You need to make a call to session_start() at the beginning of every page which requires the use of sessions. This function will create a cookie and alter the outgoing HTTP response headers and as such should be called before any output is sent from the page. And the opening <?php delimiter should have no white space before it. Session register should not be used... (PHP 4.2.0 and greater) here is a page that discusses why.

http://p2p.wrox.com/topic.asp?TOPIC_ID=2052

<?php

//file2.php

session_start();

echo $_SESSION["var1"];

// Two plus signs will post-increment and assign that value to the variable
$_SESSION["var1"]++;

?>

This method also requires that you have cookies enabled in your browser. Otherwise you will need to pass the session id to each page that requires use of sessions via a url embedded argument.

echo "<a href='file2.php?".SID."'>file 2</a>";

or

echo "<a href='file2.php?sid=".session_id()."'>file2</a>";

And finally the PHP manual page on sessions

http://www.php.net/session

: )
Rich


:::::::::::::::::::::::::::::::::
Smiling Souls
http://www.smilingsouls.net
:::::::::::::::::::::::::::::::::





Similar Threads
Thread Thread Starter Forum Replies Last Post
Session Variables in C# shikha09 C# 1 November 28th, 2006 10:38 AM
Session Variables testsubject Visual Studio 2005 8 March 8th, 2006 04:26 PM
Is it possible for me using session variables into see07 ASP.NET 1.x and 2.0 Application Design 4 March 9th, 2005 07:46 PM
Session Variables - help? Annoyamouse BOOK: Beginning PHP4/PHP 5 ISBN: 978-0-7645-4364-7; v5 ISBN: 978-0-7645-5783-5 11 August 31st, 2004 03:56 PM
session variables help face Classic ASP Databases 4 September 12th, 2003 03:57 PM





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