Page 1 of 1
php session how to pass login user name for every page
Posted: Sun Aug 29, 2010 8:29 am
by coolumanga
hi
i want to php code to pass login user name to every page as a session if not login return to the loin.php page
i want to pass login username as session name to page and if its true only can view the page other wise return to login page
Re: php session how to pass login user name for every page
Posted: Sun Aug 29, 2010 2:21 pm
by phpcip28
Make sure every page has this at the top:
So basically right after a successful login, you can have a user_id variable set inside the session and just check for that one in every other page.
Schematically it would be something like this. Please note that this is NOT the actual code you should use

It's just a guideline.
Code: Select all
session_start();
if($login == "OK"){
//Register the user_id session variable
$_SESSION['user_id'] = $user_id;
//Redirect the user to the members area protected page
}
To check that user is still logged in, you do like this in the top of every page:
Code: Select all
session_start();
if($_SESSION['user_id']){
//USER IS LOGGED IN
}else{
//USER SESSION EXPIRED OR IS NOT LOGGED IN
}
Anyway this is the basics of using php sessions, you should look into secure cookies, and why not, maybe go with
http://www.codeigniter.com
CodeIgniter is a MVC PHP Framework that has all this built-in so you don't have to worry about security related and user experience related issues.
Good luck.
Re: php session how to pass login user name for every page
Posted: Mon Aug 30, 2010 4:04 am
by coolumanga
Thankz a lot phpcip28
But i have the problem
how i coudnt get username as the session
how do i get current user as the session.
then how do i check
Re: php session how to pass login user name for every page
Posted: Mon Aug 30, 2010 4:10 am
by phpcip28
I don't understand your question really....
Re: php session how to pass login user name for every page
Posted: Mon Aug 30, 2010 5:59 am
by iijb
Hi,
How do you checking the login data.
If it is done using any database(mysql) , then some thing like this,
Login.php
Code: Select all
<?php
$link = mysql_connect("localhost", "mysql_user", "mysql_password");
mysql_select_db("databasename", $link);
$result = mysql_query("SELECT * FROM tablename WHERE username=' " . $_POST['username'] ." ' AND password=' " .$_POST['password']. " ' ", $link);
$num_rows = mysql_num_rows($result);
// checking login values are correct
if($num_rows==1)
{
$_SESSION['username']=$_POST['username']; //here assigning username of logged in user to session variable
header("location: home.php");
}
?>
And in your home page
Home.php
Code: Select all
<?php
session_start();
//check this in all pages
if(!isset($_SESSION['username']))
{
header("location: login.php");
}
echo "Welcome".$_SESSION['username']); //use this in all pages where you want username
?>
Try this
Regards
iijb
Re: php session how to pass login user name for every page
Posted: Mon Aug 30, 2010 10:02 pm
by coolumanga
Thankz iijb
i used ur code
but i get this error
Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at C:\xampp\htdocs\firefox\custom\log.php:2) in C:\xampp\htdocs\firefox\custom\basic_rep.php on line 3
can you help me to sort out
Re: php session how to pass login user name for every page
Posted: Mon Aug 30, 2010 11:05 pm
by iijb
Hi
You have to insert session_start(); in the very first line of login.php file also. I forgot to insert that. Also you should make sure nothing gets sent to the browser before session_start(); Including white space.
Regards
iijb