cdm30 Posted April 27, 2011 Share Posted April 27, 2011 Hi there, New to the forum... thank you in advance for any help! I created a login system using a tutorial found online. Everything works perfectly.. but now the client wants "Hello [First Name]" displayed after logging in. I've tried about 12 different tutorials at this point and can't seem to tweak them enough to work with my code. A lot of the tutorials have session_start(); at the top of their protected (welcome.php) page. But mine seems to be held in a variable in another script and the top of my welcome.php looks like this: <?PHP require_once("./include/membersite_config.php"); if(!$fgmembersite->CheckLogin()) { $fgmembersite->RedirectToURL("login.php"); exit; } ?> This is what I have on my welcome page as well but I don't know how to make this all come together and work.. Hello <?php echo $_SESSION['name']; ?> I'm using mySQL 5.0, and php 5.2. Will anyone shed light on this for me? I'll provide any other info you may need.. Thank you Quote Link to comment https://forums.phpfreaks.com/topic/234874-display-welcome-message-after-login/ Share on other sites More sharing options...
fugix Posted April 27, 2011 Share Posted April 27, 2011 so the username is stored in a session when they log in? what exactly is going wrong when someone logs in? Quote Link to comment https://forums.phpfreaks.com/topic/234874-display-welcome-message-after-login/#findComment-1206965 Share on other sites More sharing options...
Muddy_Funster Posted April 27, 2011 Share Posted April 27, 2011 <?PHP require_once("./include/membersite_config.php"); if(!$fgmembersite->CheckLogin()) { $fgmembersite->RedirectToURL("login.php"); exit; } if (isset($_SESSION['name'])){ $name = $_SESSION['name']; echo "Hello $name"; ?> using the isset() function to check that the session variable of ['name'] exists (which it only will while someone has logged in) lets you display the name on condition that it is a logged in member that is viewing the page. Quote Link to comment https://forums.phpfreaks.com/topic/234874-display-welcome-message-after-login/#findComment-1206972 Share on other sites More sharing options...
CK9 Posted April 27, 2011 Share Posted April 27, 2011 If the page isn't embeded in another one that already has it, you need to include session_start(); before any of the page code. This tells the server you want to access the session information that you have stored elsewhere. I made a login thing for a section of my site that does what you're looking to do if you want to take a look at it. Quote Link to comment https://forums.phpfreaks.com/topic/234874-display-welcome-message-after-login/#findComment-1206977 Share on other sites More sharing options...
fugix Posted April 27, 2011 Share Posted April 27, 2011 yes if you are using sessions to store information. you need to have session_start() on your pages before you can call any session values Quote Link to comment https://forums.phpfreaks.com/topic/234874-display-welcome-message-after-login/#findComment-1206981 Share on other sites More sharing options...
cdm30 Posted April 27, 2011 Author Share Posted April 27, 2011 Thanks guys for the info.. Muddy_Funster I used your code and added session start to top like others mentioned, I've also kept Hello < /? php echo $_SESSION['name']; ? /> in the body where I want it to display. But it's still not working... here's the top of the page now: <?PHP session_start(); require_once("./include/membersite_config.php"); if(!$fgmembersite->CheckLogin()) { $fgmembersite->RedirectToURL("login.php"); exit; } if (isset($_SESSION['name'])){ $name = $_SESSION['name']; echo "Hello $name"; } ?> membersite_config.php is where the database connection info is stored. And all the session info regarding input and login username password etc is in a page called fg_membersite.php. Might be a dumb question but shouldn't this page be in my welcome.php script some where? Since this is teh page with session variables? I don't know though since everything else works fine.. CK9 I'd love to see what you've done on your own site, thank you! Quote Link to comment https://forums.phpfreaks.com/topic/234874-display-welcome-message-after-login/#findComment-1207082 Share on other sites More sharing options...
CK9 Posted April 27, 2011 Share Posted April 27, 2011 Here's what I did while working on a project with a few other people: //this section is a bit unrelated, but I left it in so I can explain where the session_start() is at <? $sp = $_GET['pg']; function pgswtch($sp) { include("header.html"); echo "<table cols='2' border='0' cellpadding='10' cellspacing='0' align='center' width='100%'>"; echo "<tr>"; echo "<td width='16%'>"; include('navbar.html'); echo "</td><td>"; switch($sp) { case 'info': include('info.html'); break; case 'maps': include('maplist.html'); break; case 'images': include('piclist.html'); break; case 'other': include('misc.html'); break; } echo "</td></tr></table>"; include('footer.html'); } //and this is where it seems the most relevant to what you're doing. if ($_GET['u'] == '' || empty($_SESSION['user'])) { echo "You have not been given authorization to view this folder!"; } else { echo "<center><font color='00cc00' size='7'>Welcome back" . $_SESSION['user'] . "!</font></center>"; pgswtch($sp); } ?> Because I use a switch to generate a page layout similar to HTML frames without all the annoyances of frames, I was able to store the session_start() in the header.html file. It works perfectly this way across all pages accessed through the index.php page. The if statement is just a safegaurd that was in place to preven people from messing with the project. You could use it similarly to make sure the user session variable is actually recorded. In example: if(empty($_SESSION['user'])) { echo "No username has been indicated."; } If you do this and then see that message on the screen, then check the login verification that stores the data in the variable for a session_start(), as this needs to be on every page that uses session variables. Quote Link to comment https://forums.phpfreaks.com/topic/234874-display-welcome-message-after-login/#findComment-1207198 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.