Demonic Posted September 20, 2006 Share Posted September 20, 2006 Im trying to find out how to best learn sessions but I just can't seen to understand them in any way/or form.Can someone point me in a direction to where I best to look(I'm still looking on google :() Quote Link to comment https://forums.phpfreaks.com/topic/21467-i-still-dont-understand-sessions-and-i-really-need-them-at-this-point/ Share on other sites More sharing options...
DarkHavn Posted September 20, 2006 Share Posted September 20, 2006 A session is just a value you can set just like a variable and that can be be passed from page to page,Well as long as you put[code]session_start();[/code]at the start of each page that you are trying to use sessionsOk i will give you an example, say we had a form where the person could enter their first name, and the name of this input feild was 'firstname'the following script takes that value from the $_POST and stores it to a session[code]session_start(); //Remember you need this to be able to carry information //from page to page using sessionsif($_POST['firstname']){$_SESSION['firstname'] = $_POST['firstname'];//Just an example, a real script you would check the information //submitted by a form for attempts to crack/hack your database or anything //along those lines}[/code]So in that script, i have stores the information the user supplied in the form, (their first name) and set it to a session, so say in another page i want to access that information again, then you can by typing out that session$_SESSION['firstname'] //always holds the information that was in the form unless you unset it Quote Link to comment https://forums.phpfreaks.com/topic/21467-i-still-dont-understand-sessions-and-i-really-need-them-at-this-point/#findComment-95696 Share on other sites More sharing options...
Demonic Posted September 20, 2006 Author Share Posted September 20, 2006 Just like cookies basically?So how can I make a page like generate so everytime you leave the page you have to login.Basically an Admin CP page like InvisionPowerBoard 2.1.7 and other versions. You have to login everytime you leave page.How do I do somthen like that? Quote Link to comment https://forums.phpfreaks.com/topic/21467-i-still-dont-understand-sessions-and-i-really-need-them-at-this-point/#findComment-95704 Share on other sites More sharing options...
DarkHavn Posted September 20, 2006 Share Posted September 20, 2006 Well with that you can go two ways.Having the session time out after so long, so if the person doesn't manually log out, then after a few minutes they are logged out, or prompted or what not.Everytime they log onset a session$_SESSION['loggedon'] = trueafter you have checked they are an actual user in the database.Then carry on to when the log out$_SESSION['loggedon'] = falseand do checks likeif($_SESSION['loggedon'] == true){//then do this html} else {//user isn't logged on//login form}phpfreaks offer some good tutorials Quote Link to comment https://forums.phpfreaks.com/topic/21467-i-still-dont-understand-sessions-and-i-really-need-them-at-this-point/#findComment-95705 Share on other sites More sharing options...
newb Posted September 20, 2006 Share Posted September 20, 2006 sessions arent hard to get once u learn the concept of them, and are quite easier to use than cookies imho.[quote]So how can I make a page like generate so everytime you leave the page you have to login.[/quote]session variables automatically are unset when a user closes the browser i believe. Quote Link to comment https://forums.phpfreaks.com/topic/21467-i-still-dont-understand-sessions-and-i-really-need-them-at-this-point/#findComment-95706 Share on other sites More sharing options...
DarkHavn Posted September 20, 2006 Share Posted September 20, 2006 Really?I thought the sessions are unset depending on the information you supplynewb check out the php_info(); (i think that's the function) and check out the information im sure somewhere in there it has the sessions that has the time out value to when they are unset,as i have servers set up at home for testing and when coding i'm always having to clear my browser due to the sessions for testing reasons Quote Link to comment https://forums.phpfreaks.com/topic/21467-i-still-dont-understand-sessions-and-i-really-need-them-at-this-point/#findComment-95708 Share on other sites More sharing options...
onlyican Posted September 20, 2006 Share Posted September 20, 2006 sessions die when the browser session ends, hint the name sessionsjust please rememberLine 1 of your codeBefore any HTM code (<html>)you NEED<?php session_start(); ?>[quote author=DarkHavn link=topic=108851.msg438348#msg438348 date=1158790007]Really?I thought the sessions are unset depending on the information you supplynewb check out the php_info(); (i think that's the function) and check out the information im sure somewhere in there it has the sessions that has the time out value to when they are unset,as i have servers set up at home for testing and when coding i'm always having to clear my browser due to the sessions for testing reasons[/quote]That is to do with the timeIf I dont close my browser, and the time has passed, which is set in the php.ini, then the session will still dieand the function is phpinfo(); (no underscore)The sessions can still be there, if not stored as a temp, but they are not valid Quote Link to comment https://forums.phpfreaks.com/topic/21467-i-still-dont-understand-sessions-and-i-really-need-them-at-this-point/#findComment-95710 Share on other sites More sharing options...
newb Posted September 20, 2006 Share Posted September 20, 2006 i think both of our points are quite valid actually. sessions indeed have a timeout value to when they are unset(for instance: if a user does not close their browser and leaves his browser running idle, it will time out) and the sessions automatically unset every time the user closes his browser due to the fact that sessions are stored on the webserver(aside from the phpsessid), unlike cookies(where the information is stored on the client's hard disk). Quote Link to comment https://forums.phpfreaks.com/topic/21467-i-still-dont-understand-sessions-and-i-really-need-them-at-this-point/#findComment-95712 Share on other sites More sharing options...
Demonic Posted September 20, 2006 Author Share Posted September 20, 2006 [code]<?php session_start(); ?><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN" "http://www.w3.org/TR/html4/frameset.dtd"><HTML><HEAD><TITLE>Admin Control Panel</TITLE></HEAD><?phpinclude "config.php";if($logged[level] != 5){echo "You do not have access to this page";exit();}?><?php$acpuname = no_injection($_POST['username']);$password = md5($_POST['password']);$_SESSION['username'] = $acpuname;$_SESSION['password'] = $password;if($_SESSION['username'] != true && $_SESSION['password'] !=true){echo "<form method='post'><table colspan='2'><tr><td>Name:</td><td><input type='text' name='username'></td></tr><tr><td>Password:</td><td><input type='password' name='password'></td></tr></table><input type='submit' name='login'></form>";$result = mysql_query("SELECT * FROM users WHERE username='$acpuname' AND password='$password' ");$true = mysql_fetch_array($result);}if(isset($_POST['submit'])){if($true[username] == $acpuname){$_SESSION['username'] = true;}if($true[password] == $password){$_SESSION['password'] = true;}}if($_SESSION['username'] == true AND $_SESSION['password'] == true){?><frameset rows="20%,80%" border="0"><frame src="acpb.php" noresize="noresize" border="0"><frameset cols="25%,75%" border="0"><frame src="content.php" noresize="noresize" border="0"><frame src="admin.php" name="showframe" noresize="noresize" border="0"></frameset></frameset></HTML><? } ?>[/code]I Written that code and I get a blank page I don't think I have written it correctly.Now I used and else statement and when i close window or leave page and go back to it it still shows page?[code]<?php session_start(); ?><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN" "http://www.w3.org/TR/html4/frameset.dtd"><HTML><HEAD><TITLE>Admin Control Panel</TITLE></HEAD><?phpinclude "config.php";if($logged[level] != 5){echo "You do not have access to this page";exit();}?><?php$acpuname = no_injection($_POST['username']);$password = md5($_POST['password']);$_SESSION['username'] = $acpuname;$_SESSION['password'] = $password;if($_SESSION['username'] != true && $_SESSION['password'] !=true){echo "<form method='post'><table colspan='2'><tr><td>Name:</td><td><input type='text' name='username'></td></tr><tr><td>Password:</td><td><input type='password' name='password'></td></tr></table><input type='submit' name='login'></form>";$result = mysql_query("SELECT * FROM users WHERE username='$acpuname' AND password='$password' ");$true = mysql_fetch_array($result);}if(isset($_POST['submit'])){if($true[username] == $acpuname){$_SESSION['username'] == true;}if($true[password] == $password){$_SESSION['password'] == true;}}else{?><frameset rows="20%,80%" border="0"><frame src="acpb.php" noresize="noresize" border="0"><frameset cols="25%,75%" border="0"><frame src="content.php" noresize="noresize" border="0"><frame src="admin.php" name="showframe" noresize="noresize" border="0"></frameset></frameset></HTML><?php } ?>[/code]whats wrong? Quote Link to comment https://forums.phpfreaks.com/topic/21467-i-still-dont-understand-sessions-and-i-really-need-them-at-this-point/#findComment-95731 Share on other sites More sharing options...
newb Posted September 20, 2006 Share Posted September 20, 2006 pretty sloppy, mess around with it a bit, and make it neater while ur at it..hurts my eyes to look at. Quote Link to comment https://forums.phpfreaks.com/topic/21467-i-still-dont-understand-sessions-and-i-really-need-them-at-this-point/#findComment-95737 Share on other sites More sharing options...
Demonic Posted September 20, 2006 Author Share Posted September 20, 2006 ??? I can't get the shit to work been messing around with it for a while. Any body know whats wrong? Quote Link to comment https://forums.phpfreaks.com/topic/21467-i-still-dont-understand-sessions-and-i-really-need-them-at-this-point/#findComment-95740 Share on other sites More sharing options...
Demonic Posted September 21, 2006 Author Share Posted September 21, 2006 bump Quote Link to comment https://forums.phpfreaks.com/topic/21467-i-still-dont-understand-sessions-and-i-really-need-them-at-this-point/#findComment-95768 Share on other sites More sharing options...
Demonic Posted September 21, 2006 Author Share Posted September 21, 2006 bump again O_O Quote Link to comment https://forums.phpfreaks.com/topic/21467-i-still-dont-understand-sessions-and-i-really-need-them-at-this-point/#findComment-95841 Share on other sites More sharing options...
onlyican Posted September 21, 2006 Share Posted September 21, 2006 Do u know, all the bumping ur doing, ur less likly to get a responce. As it makes more postsAnywayFrom the First Script[quote]<?phpinclude "config.php";if($logged[level] != 5){echo "You do not have access to this page";exit();}?>[/quote]You need to call the session, and call the key with quotes, or it will look for a Constantif($_SESSION["logged"] != 5){is no_injection your own function? I aint heard of it, and cant find it on php.netAlso, Where are ytour HTM body tags<body></body>After </head>Before </html>Code Block 2again as I mentioned above$logged[level][quote]if($true[username] == $acpuname){[/quote]Again, its looking for the Defined value of username, not the value username, you need quotesand if its a session, then $_SESSIONAlso RememberSessions dont always die as soon as the browser is closedClosed ALL browser activities, not just your siteGo have a smoke, make a cup of tea or somethingThen try Quote Link to comment https://forums.phpfreaks.com/topic/21467-i-still-dont-understand-sessions-and-i-really-need-them-at-this-point/#findComment-95927 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.