u214 Posted June 7, 2011 Share Posted June 7, 2011 So yeah, I still cannot understand how exactly are sessions used. I tried it in the past, also just a few hours ago. To my luck, it all failed. Yes, I've been using tutorials, but still, I don't get it. :/ Anyways, I was wondering if anyone can help me out, or at least help me out make this work using a session: Let's start off with the log in form: .. Other html stuff .. (No php code above the form) <form action="Logged.php" method="post"> <li>Name:<input type="text" name="User" /></li> <li>Pass: <input type="password" name="Password" /></li> <input type="submit" value="Login" /> <input type="reset" value="Clear" /></form> .. Rest of html stuff.. (No php code below the form) Ok, once you hit "Login", it takes you to the Logged.php page and it shows your statistics. Once you leave that page, you are signed out. I want to know how to stay connected ( logged ) on ALL pages I have. Quote Link to comment https://forums.phpfreaks.com/topic/238614-help-understanding-php-sessions/ Share on other sites More sharing options...
revraz Posted June 7, 2011 Share Posted June 7, 2011 You need to use session_start at the top of each page you want to use sessions. Then you store data in the $_SESSION variable that you want to use on each page that uses sessions. Quote Link to comment https://forums.phpfreaks.com/topic/238614-help-understanding-php-sessions/#findComment-1226247 Share on other sites More sharing options...
Zane Posted June 7, 2011 Share Posted June 7, 2011 SESSIONS is a superglobal. Just like SERVER, COOKIE, POST and GET, it is saved for a period of time, whether it be in a URL or in a POST form submission... the superglobals are there. The $_SESSION variable is a bit different though. It's difference is that it retains everything in its memory until the user's browser is closed, but in order to access the information in it, you must call the PHP function: session_start By calling this function you can: - set values - ... and retrieve values The main difference in SESSIONS and COOKIES, is that: - COOKIES are stored on the client's computer (the person with the browser) - SESSIONS are stored on the webserver... AKA the website. Once you have called session_start().. setting and getting SESSION variables are as easy as: session_start(); $my_name = $_SESSION['fname']; $_SESSION['fullname'] = $myname . " Smith"; Quote Link to comment https://forums.phpfreaks.com/topic/238614-help-understanding-php-sessions/#findComment-1226255 Share on other sites More sharing options...
u214 Posted June 7, 2011 Author Share Posted June 7, 2011 Thanks for the info guys. Well, I'm just testing the functions, and I think I got something going. <?php session_start(); echo "Hi<br />"; if(!isset($_SESSION['Username'])) { echo "You are not logged in!"; echo "<br />Please login below:"; } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head profile="http://gmpg.org/xfn/11"> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>» Test «</title> </head> <body> <?php if(!isset($_SESSION['Username'])) { echo " <form method='post' action='Logged.php'> <input type='text' value='' name='User' /> <input type='password' name='Password' /> <input type='submit' value='Login' /> <input type='reset' value='Clear' /> </form>"; } else echo "Congratz! You're still logged in "; ?> <?php if(isset($_SESSION['Username'])) { echo "<br /><br />Log me out.... <a href='http://Loggedout.php'>CLICK ME </a>"; } ?> </form> </body> </html> That would be the main page. Is that how its supposed to be? If so, let me know!! It's currently working, but if there other methods in doing that, let me know!! Quote Link to comment https://forums.phpfreaks.com/topic/238614-help-understanding-php-sessions/#findComment-1226260 Share on other sites More sharing options...
Zane Posted June 7, 2011 Share Posted June 7, 2011 You've got it right,.. but my advice would be to set a variable in SESSION called is_logged or auth or something that let's you use a boolean value. This way you can do this session_start(); echo "Hi "; if(isset($_SESSION['Username']) && $_SESSION['auth']) { echo "You are not logged in!"; echo " Please login below:"; } ?> But using that method, you should immediately have $_SESSION['auth'] set false by default until logged in... then set it true.. Doing this will allow you to know the last user logged in from the session... AND whether they are still logged in or not. Because, it is possible for a person to login AND logout in one browser session. Quote Link to comment https://forums.phpfreaks.com/topic/238614-help-understanding-php-sessions/#findComment-1226268 Share on other sites More sharing options...
u214 Posted June 7, 2011 Author Share Posted June 7, 2011 Thank for the tip! I'll take that into consideration. I also made a very simple logged in panel, it's working fine as of right now!! Quote Link to comment https://forums.phpfreaks.com/topic/238614-help-understanding-php-sessions/#findComment-1226272 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.