php session how to pass login user name for every page

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
coolumanga
Forum Newbie
Posts: 9
Joined: Wed Aug 18, 2010 11:21 pm

php session how to pass login user name for every page

Post 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
User avatar
phpcip28
Forum Newbie
Posts: 22
Joined: Sun Aug 29, 2010 1:38 pm
Location: NewYork
Contact:

Re: php session how to pass login user name for every page

Post by phpcip28 »

Make sure every page has this at the top:

Code: Select all

session_start();
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.
coolumanga
Forum Newbie
Posts: 9
Joined: Wed Aug 18, 2010 11:21 pm

Re: php session how to pass login user name for every page

Post 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
User avatar
phpcip28
Forum Newbie
Posts: 22
Joined: Sun Aug 29, 2010 1:38 pm
Location: NewYork
Contact:

Re: php session how to pass login user name for every page

Post by phpcip28 »

I don't understand your question really....
User avatar
iijb
Forum Commoner
Posts: 43
Joined: Wed Nov 26, 2008 11:34 pm

Re: php session how to pass login user name for every page

Post 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
coolumanga
Forum Newbie
Posts: 9
Joined: Wed Aug 18, 2010 11:21 pm

Re: php session how to pass login user name for every page

Post 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
User avatar
iijb
Forum Commoner
Posts: 43
Joined: Wed Nov 26, 2008 11:34 pm

Re: php session how to pass login user name for every page

Post 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
Post Reply