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
php session how to pass login user name for every page
Moderator: General Moderators
-
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
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.
To check that user is still logged in, you do like this in the top of every page:
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.
Code: Select all
session_start();Schematically it would be something like this. Please note that this is NOT the actual code you should use
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
}
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
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
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
I don't understand your question really....
Re: php session how to pass login user name for every page
Hi,
How do you checking the login data.
If it is done using any database(mysql) , then some thing like this,
Login.php
And in your home page
Home.php
Try this
Regards
iijb
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");
}
?>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
?>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
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
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
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
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