m1th Posted August 1, 2011 Share Posted August 1, 2011 Hi I need to make a session if the URL paremeter has source=aw in the string. I have this code if ($_REQUEST["source"]=="aw"){ $_SESSION['affon']=1; } Then on any other PHP page or the same page on the same server folder I try echoing the file with no luck. echo $_SESSION["affon"]; Quote Link to comment https://forums.phpfreaks.com/topic/243476-php-session-wont-echo/ Share on other sites More sharing options...
the182guy Posted August 1, 2011 Share Posted August 1, 2011 You need to initialise the session at the start of any script where you want to read or write to the sesssion. Put this at the top: session_start(); Why have you got () after the echo? Should just be echo $_SESSION["affon"]; Quote Link to comment https://forums.phpfreaks.com/topic/243476-php-session-wont-echo/#findComment-1250181 Share on other sites More sharing options...
m1th Posted August 1, 2011 Author Share Posted August 1, 2011 The script already has includes in the header which have session_start(); inside them .. there are a lot of files around with session start so there would be no conflict?? Quote Link to comment https://forums.phpfreaks.com/topic/243476-php-session-wont-echo/#findComment-1250187 Share on other sites More sharing options...
Drummin Posted August 1, 2011 Share Posted August 1, 2011 I think your () are killing it. Try if ($_REQUEST["source"]=="aw"){ $_SESSION['affon']=1; } echo $_SESSION["affon"]; Quote Link to comment https://forums.phpfreaks.com/topic/243476-php-session-wont-echo/#findComment-1250237 Share on other sites More sharing options...
m1th Posted August 1, 2011 Author Share Posted August 1, 2011 Yes, I know. That is already working and I copied and pasted something else, that is why the (); came in.. Quote Link to comment https://forums.phpfreaks.com/topic/243476-php-session-wont-echo/#findComment-1250240 Share on other sites More sharing options...
Drummin Posted August 1, 2011 Share Posted August 1, 2011 Can you hard code a session value and pass that from page to page? If not it's a session_start(); issue. Quote Link to comment https://forums.phpfreaks.com/topic/243476-php-session-wont-echo/#findComment-1250245 Share on other sites More sharing options...
spiderwell Posted August 1, 2011 Share Posted August 1, 2011 if you get an undefined index error when echoing, its session_start() Quote Link to comment https://forums.phpfreaks.com/topic/243476-php-session-wont-echo/#findComment-1250249 Share on other sites More sharing options...
m1th Posted August 1, 2011 Author Share Posted August 1, 2011 I can have this: /site/john.php Source: <?php session_start(); if ($_REQUEST["source"]=="aw"){ $_SESSION['affon']=1; } ?> If I try now making a file on the same server. Example /site/sub/sarah.php Source: <?php echo $_SESSION["affon"]; ?> The file won't work and won't register that a session is on the server. I thought PHP has it's own Memory?? Quote Link to comment https://forums.phpfreaks.com/topic/243476-php-session-wont-echo/#findComment-1250250 Share on other sites More sharing options...
Drummin Posted August 1, 2011 Share Posted August 1, 2011 What happens if you add this to your pages session_start(); if ($_REQUEST['source']=="aw"){ $_SESSION['affon']=1; } else{ $_SESSION['affon']=0; } echo $_SESSION['affon']; Quote Link to comment https://forums.phpfreaks.com/topic/243476-php-session-wont-echo/#findComment-1250257 Share on other sites More sharing options...
the182guy Posted August 1, 2011 Share Posted August 1, 2011 /site/sub/sarah.php Source: <?php echo $_SESSION["affon"]; ?> The file won't work and won't register that a session is on the server. I thought PHP has it's own Memory?? As I said above, you need session_start() at the top of all scripts, that's why it isn't working. Quote Link to comment https://forums.phpfreaks.com/topic/243476-php-session-wont-echo/#findComment-1250268 Share on other sites More sharing options...
m1th Posted August 2, 2011 Author Share Posted August 2, 2011 So I need the code EVEN though I have already started the session? ASP (Classic) all you do is <% session("aff_code") = request("source") %> And you can use that on the IIS server untill you run session.abandon(); Quote Link to comment https://forums.phpfreaks.com/topic/243476-php-session-wont-echo/#findComment-1250575 Share on other sites More sharing options...
voip03 Posted August 2, 2011 Share Posted August 2, 2011 Have you registered the session? Quote Link to comment https://forums.phpfreaks.com/topic/243476-php-session-wont-echo/#findComment-1250577 Share on other sites More sharing options...
m1th Posted August 2, 2011 Author Share Posted August 2, 2011 What do you mean registered the session? I am just saying is there no other way to have session calls through the server on other PHP files without using the session start at the top.... That is all I wanted to know. I just want to know why PHP you need to declare a session_start(); every file. I used to code PHP a long time ago; I remember the nasty ?PHPSESSIONID and other URLs which I definatley do not want users or search engines to see on my app Quote Link to comment https://forums.phpfreaks.com/topic/243476-php-session-wont-echo/#findComment-1250578 Share on other sites More sharing options...
voip03 Posted August 2, 2011 Share Posted August 2, 2011 PHP session solves this problem by allowing you to store user information on the server for later use (i.e. username, shopping cart items, etc). However, this session information is temporary and is usually deleted very quickly after the user has left the website that uses sessions. It is important to ponder if the sessions' temporary storage is applicable to your website. If you require a more permanent storage you will need to find another solution, like a MySQL database. Sessions work by creating a unique identification(UID) number for each visitor and storing variables based on this ID. This helps to prevent two users' data from getting confused with one another when visiting the same webpage. Note:If you are not experienced with session programming it is not recommended that you use sessions on a website that requires high-security, as there are security holes that take some advanced techniques to plug. Starting a PHP Session Before you can begin storing user information in your PHP session, you must first start the session. When you start a session, it must be at the very beginning of your code, before any HTML or text is sent. http://php.net/manual/en/function.session-start.php http://www.w3schools.com/php/php_sessions.asp Ps. Learn to google it; you will get more more answer. Quote Link to comment https://forums.phpfreaks.com/topic/243476-php-session-wont-echo/#findComment-1250580 Share on other sites More sharing options...
m1th Posted August 2, 2011 Author Share Posted August 2, 2011 Thanks guys! Quote Link to comment https://forums.phpfreaks.com/topic/243476-php-session-wont-echo/#findComment-1250583 Share on other sites More sharing options...
voip03 Posted August 2, 2011 Share Posted August 2, 2011 please mark as solved. the topic solved button can be found at the bottom left of the page Thank you Quote Link to comment https://forums.phpfreaks.com/topic/243476-php-session-wont-echo/#findComment-1250586 Share on other sites More sharing options...
m1th Posted August 2, 2011 Author Share Posted August 2, 2011 Yes, I was looking for the this. you need to get the Invision Power Board quick reply mod too! Thanks! hope to return fav in css/xhtml EDIT - just seen quick reply mod. Quote Link to comment https://forums.phpfreaks.com/topic/243476-php-session-wont-echo/#findComment-1250587 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.