_giles_ Posted December 14, 2006 Share Posted December 14, 2006 Hi all,I’m working on my first project with sessions, kind of getting stuck on what is probably a simple point. Would you mind correcting me? My understanding is that I can declare the session variables in an include file such as<?php if (!isset($_SESSION["count"])) { $_SESSION["start"] = time(); $_SESSION["count"] = 1; $_SESSION["counthistory"] = 1; $_SESSION["lastpage"] = 100; } $_SESSION["count"]++;?>and that I can then access the session from other files such as : <?php require_once "admin.inc";session_start();echo "session_id() is : " . session_id();echo "</br>";echo $_SESSION["count"];?>this is partly working, in that I can return the session_id, but I get a “Undefined index: count” with the count parameter. what am I doing wrong?I’d appreciate any help you could give me.Thanks in advanceGiles Link to comment https://forums.phpfreaks.com/topic/30670-solved-trouble-with-sessions-and-file-locations/ Share on other sites More sharing options...
hitman6003 Posted December 14, 2006 Share Posted December 14, 2006 the session_start() must be above where ever you are using any $_SESSION variable. If you are using sessions, it is generally safe to ALWAYS MAKE SESSION_START() THE FIRST THING IN YOUR SCRIPT. Link to comment https://forums.phpfreaks.com/topic/30670-solved-trouble-with-sessions-and-file-locations/#findComment-141385 Share on other sites More sharing options...
_giles_ Posted December 14, 2006 Author Share Posted December 14, 2006 thanks for the quick response ... works a treat! guess the only thing leaving me a little puzzled is why do ALL the tutorials I've been working on put the include files at the top seemingly without getting into trouble? I thought I needed to put a reference to the file where I have the session variables before I try use them? Link to comment https://forums.phpfreaks.com/topic/30670-solved-trouble-with-sessions-and-file-locations/#findComment-141448 Share on other sites More sharing options...
alpine Posted December 15, 2006 Share Posted December 15, 2006 You can also set session_start() on top inside your admin.inc file, then it would always be there but just written once.When that is said, i would also recomend to rename the extention in admin.inc from .inc to .php since admin.inc probably contains config settings ? ? Just a safety issue (just ?!) Link to comment https://forums.phpfreaks.com/topic/30670-solved-trouble-with-sessions-and-file-locations/#findComment-141583 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.