ConnorMcF Posted January 26, 2015 Share Posted January 26, 2015 (edited) What I'm trying to do is get my $_SESSION to work throughout my website. I'm quite new to PHP, so the PHP Manual didn't make much sense to me, so I thought I'd post here! ^^ I've got my login script under /session/ and I want it to be able to display your username on the homepage (/), but it only works inside /session/. If you are wondering, I am using PHP-Login Advanced. Thank you, - Connor! Edited January 26, 2015 by ConnorMcF Quote Link to comment https://forums.phpfreaks.com/topic/294247-_session-throughout-entire-domain/ Share on other sites More sharing options...
CroNiX Posted January 26, 2015 Share Posted January 26, 2015 session_start() needs to be at the very top of all of your pages, or else the session data won't be available to that page/script. Quote Link to comment https://forums.phpfreaks.com/topic/294247-_session-throughout-entire-domain/#findComment-1504285 Share on other sites More sharing options...
Tom8001 Posted January 30, 2015 Share Posted January 30, 2015 (edited) Yeah like CroNiX said if you do not have session_start() and you try to use $_SESSION your going to get an error, and have you tried making a functions.php file and adding session expressions into it? because then you can just include the functions.php file into all the other .php files. Also if you want to display the username do the following i'll add security in this example: <?php //Get values sent from form $username = $_POST['username']; $password = $_POST['password']; //Strip tags from string $username = strip_tags($username); $password = strip_tags($password); //Encrypt password $password = hash('fnv164', $password); //Escape from HTML $username = mysqli_real_escape_string($username); $password = mysqli_real_escape_string($password); //Convert special characters $username = htmlspecialchars($username, ENT_QUOTES); $password = htmlspecialchars($password, ENT_QUOTES); //If you are on another page and you want to echo the username session_start(); //Critical this MUST be at the top of the page. //Check if SESSION is set if(isset($_SESSION[''])) { $_SESSION['username'] = $username; } echo "Welcome, ".$username."!"; ?> Not sure if i went to over the top there but hopefully it should help you out Edited January 30, 2015 by Tom8001 Quote Link to comment https://forums.phpfreaks.com/topic/294247-_session-throughout-entire-domain/#findComment-1504381 Share on other sites More sharing options...
NotionCommotion Posted January 30, 2015 Share Posted January 30, 2015 What I'm trying to do is get my $_SESSION to work throughout my website. I'm quite new to PHP, so the PHP Manual didn't make much sense to me, so I thought I'd post here! ^^ I've got my login script under /session/ and I want it to be able to display your username on the homepage (/), but it only works inside /session/. If you are wondering, I am using PHP-Login Advanced. Thank you, - Connor! Likely, the cookie associated with your cookie is only available to /session/ and other children directories. Try using something like session_set_cookie_path() or session_save_path(). Quote Link to comment https://forums.phpfreaks.com/topic/294247-_session-throughout-entire-domain/#findComment-1504388 Share on other sites More sharing options...
CroNiX Posted January 30, 2015 Share Posted January 30, 2015 There's nothing in that script that changes the location of session cookies or save paths. Most likely the OP just isn't using $_SESSION properly on his new custom pages outside of that login script. Quote Link to comment https://forums.phpfreaks.com/topic/294247-_session-throughout-entire-domain/#findComment-1504393 Share on other sites More sharing options...
NotionCommotion Posted January 30, 2015 Share Posted January 30, 2015 I didn't look at the script, but he did say it works under /session/ but not the homepage (/). Quote Link to comment https://forums.phpfreaks.com/topic/294247-_session-throughout-entire-domain/#findComment-1504395 Share on other sites More sharing options...
CroNiX Posted January 30, 2015 Share Posted January 30, 2015 Yeah, that's where he installed the login library. So it works there, but now the OP wants to use the sessions generated by logging in on OTHER pages. Quote Link to comment https://forums.phpfreaks.com/topic/294247-_session-throughout-entire-domain/#findComment-1504397 Share on other sites More sharing options...
Werezwolf Posted February 5, 2015 Share Posted February 5, 2015 (edited) As all previous posters above have stated make sure that you start your session <?php if(!session_id()){session_start();} ?> EDIT: Actually you might not of installed PHP-Login Advanced correctly. As i have not used it myself of the installation options, you may want to try reinstalling that before you move on to my next suggestion. If there was anything about session cookies or directories you may want to keep them at default. If that dose not work you php.ini may not be configured correctly i would edit that first or if you cant your Last option is. <?php session_set_cookie_params (0,'/','mywebsite.com',0,0); if(!session_id()){session_start();} ?> For more information on session_set_cookie_params http://php.net/manual/en/function.session-set-cookie-params.php --OffTopic Dose anyone have a problem with $_SERVER['SERVER_NAME'] as a catch all ? As it may be a better suggestion session_set_cookie_params (0,'/',$_SERVER['SERVER_NAME'],0,0); Edited February 5, 2015 by Werezwolf Quote Link to comment https://forums.phpfreaks.com/topic/294247-_session-throughout-entire-domain/#findComment-1504934 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.