Jump to content

Sessions Understanding


tahakirmani

Recommended Posts

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 by tahakirmani
Link to comment
Share on other sites

$_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 by SocialCloud
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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');

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.