tahakirmani Posted December 1, 2012 Share Posted December 1, 2012 Hi I have created a program in which i create a user session when he logs in. I am following Newboston tutorials. I have created the program, but i am unable to understand that how session works. I have created 3 different php files, Index.php,core.php, email_login.php. Following are my index.php file & core.php file. <?php $current_file=$_SERVER['SCRIPT_NAME']; ob_start(); session_start(); ?> <?php include 'core.php' ; if (isset($_SESSION['user_id'])) { echo "You are logged in "; include 'welcome.php'; //echo "Welcome " . $user_name; } else { include 'email_login.php'; } The only thing i am unable to understand here is that how does index.php file come to know about $_SESSION['user_id'], because i used this '$_SESSION['user_id']' in email_login.php , and i include 'email_login.php' in else statement. Kindly guide me about its processing. Thanks Taha. Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted December 1, 2012 Share Posted December 1, 2012 Your question actually has nothing to do with session variables, it has to do with variable scope. When you use an include statement, the content of the included file virtually becomes part of the code at the point of the include statement and inherits any existing variables in the scope where the include statement is at. An include statement in your main program's scope inherits any existing variables in your main program's scope. An include statement in a function or class inherits any existing variables in the scope of that function or class. Quote Link to comment Share on other sites More sharing options...
tahakirmani Posted December 1, 2012 Author Share Posted December 1, 2012 Thank you so much for your reply. In core.php file i dont have any variable " $_SESSION['user_id'] " . i created this variable on "email_login.php" than i am including in else statement. When it reads if statement it does not know anything about 'email_login.php' & ''user_id' variable then how come it knows about it and executes it. Quote Link to comment Share on other sites More sharing options...
MDCode Posted December 1, 2012 Share Posted December 1, 2012 $_SESSION variables are set until the user "logs out." It will be available on every page of your site. Quote Link to comment Share on other sites More sharing options...
tahakirmani Posted December 2, 2012 Author Share Posted December 2, 2012 (edited) Thanks for your help. I am still unable to understand how actually session works. I have read couple of articles but didnt undestand much. Kindly tell me the function of $_SEESION. What type of variable do i need to define in $_SESSIOn['']=variable ? and where does it store in these variables, and the variable inside this are user defined or system defined. I know my question is not making much sense, but i am soo much confused with this session thing. Kindly guide me . Thanks Edited December 2, 2012 by tahakirmani Quote Link to comment Share on other sites More sharing options...
MDCode Posted December 2, 2012 Share Posted December 2, 2012 (edited) $_SESSION variables are variables, defined by your site. It is used in every php based login system to define all information about a user. To set a session variables you can do: <?php $_SESSION['username'] = asdf; ?> When used as: <?php // This command (session_start() should be the very first line of your document (after <?php of course), and should not be after any html session_start(); echo "Hello ".$_SESSION['username']."! "; ?> Will display: Hello asdf! Edited December 2, 2012 by SocialCloud Quote Link to comment Share on other sites More sharing options...
tahakirmani Posted December 2, 2012 Author Share Posted December 2, 2012 Thank you so much for the guidance, i understand it, but its giving me an error message. Notice: Undefined index: username in F:\xampp\htdocs\Email_address\session_test.php Quote Link to comment Share on other sites More sharing options...
Christian F. Posted December 2, 2012 Share Posted December 2, 2012 http://php.net/sessions <- That'll give you all the info you need. Quote Link to comment Share on other sites More sharing options...
MDCode Posted December 2, 2012 Share Posted December 2, 2012 Thank you so much for the guidance, i understand it, but its giving me an error message. Notice: Undefined index: username in F:\xampp\htdocs\Email_address\session_test.php That error doesn't really matter. But you need to set the session first in the first code block I posted. Quote Link to comment Share on other sites More sharing options...
DavidAM Posted December 2, 2012 Share Posted December 2, 2012 That error doesn't really matter. But you need to set the session first in the first code block I posted. All errors matter! That includes Warnings and Notices. These all stem from problems in the code. That notice indicates you tried to retrieve a value from an element of an array and the element was not defined. Which means you did not get the value from the array, you got a NULL value. @OP: Post that section of the code and we can help. Usually this kind of thing comes from trying to retrieve a value from the session that has not been put into the session because it is the visitors first visit. You usually want to apply some meaningful default value as in: if (isset($_SESSION['username'])) $displayName = $_SESSION['username']; else $displayName = 'Guest'; which can be shortened using the ternary operator to: $displayName = (isset($_SESSION['username']) ? $_SESSION['username'] : 'Guest'); Quote Link to comment 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.