spires Posted February 11, 2008 Share Posted February 11, 2008 Hi Guys. I'm trying to create a session inside a function. At the top of every page I have <?PHP tracking(); ?> which calls in the function. What should happen is on the first page that the user comes to (From Adwords) will place the keyword in to a session (page?kw=keyword). Then if the $_GET[kw] is not present, it will display the session. How ever, it's not working. Below is the code i'm trying to get to work. Please could you advice what I need to do. Thanks This is the function. <?PHP function tracking(){ session_start(); $get_Keyword = $_GET['kw']; if (get_Keyword){ $_SESSION['key'] = $get_Keyword; echo $_SESSION['key']; }else{ echo $_SESSION['key']; } } ?> Link to comment https://forums.phpfreaks.com/topic/90533-sessions-inside-functions/ Share on other sites More sharing options...
rhodesa Posted February 11, 2008 Share Posted February 11, 2008 you are missing a $ in front of your variable in your IF statement, here is a slightly cleaner version of the code too <?php function tracking(){ session_start(); if ($_GET['kw']) $_SESSION['key'] = $_GET['kw']; echo $_SESSION['key']; } ?> Link to comment https://forums.phpfreaks.com/topic/90533-sessions-inside-functions/#findComment-464157 Share on other sites More sharing options...
dommer43 Posted February 11, 2008 Share Posted February 11, 2008 mark me as a noob for this one...but I never thought of put sessions inside functions for some reason...this could prove interesting. neat Link to comment https://forums.phpfreaks.com/topic/90533-sessions-inside-functions/#findComment-464183 Share on other sites More sharing options...
spires Posted February 11, 2008 Author Share Posted February 11, 2008 What an Idiot ! Thanks for pointing that out to me Link to comment https://forums.phpfreaks.com/topic/90533-sessions-inside-functions/#findComment-464185 Share on other sites More sharing options...
spires Posted February 11, 2008 Author Share Posted February 11, 2008 Yeah, I though the same thing dommer43. It's proving to be a bit more difficult than standard SESSIONS though. Either that, or I'm just doing it all wrong Good luck :-) Link to comment https://forums.phpfreaks.com/topic/90533-sessions-inside-functions/#findComment-464187 Share on other sites More sharing options...
rhodesa Posted February 11, 2008 Share Posted February 11, 2008 All of the Reserved Variables are in the global scope. So you can access them inside functions/classes/etc Link to comment https://forums.phpfreaks.com/topic/90533-sessions-inside-functions/#findComment-464201 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.