FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


help with login to display the current username





syntax101
i already created a login script with user authentication and logout the problem is
when i login i cannot see the username to the link if it is succesful

can anybody help me display the username after i log in (i want to pass the inputed variable in the username textfield to another page so i can view the username ) which is after the succesful log in
coeus
Are you not using sessions? And/or cookies?
They allow you to pass variables around your various pages on a per user basis.
DjinniFire
Well on login I assume you have something setup like so:

=============

User inputs his or her username and password. Submits form.

Form then sends POST data to a script that runs it through MySQL or whatever database type to check that the username exists and that if it does the password is correct. Once doing so you login.

=============

Now to pass the variable along make sure on top of every page you start with
Code:
session_start();


Which will begin your sessions which I assume you are setting?
If not do this
Code:
<?php
session_start();
// POST data set to variables
    // MySQL code to check username and password
     $query=mysql_query("SELECT * FROM table WHERE username='$username' AND password='$password'");
     if(isset($query)) {
             $_SESSION['username']=$username;
     }
     else {
             echo "Sorry wrong username or password. Or user does not exist.";
     }
?>


Now on all your pages you are passing along the session variable (Same concept with cookies I believe you need ob_start(); on top of every page if you use it ...I haven't worked with cookies much so I think it's ob_start()Wink

Remember session_start(); at the TOP very top of every page. Then in middle of your pages you can do
Code:
<?php echo "Hello ".$_SESSION['username']."!"; ?>
And that'll display Hello USERNAME! And it'll mean they are logged in.
syntax101
thank u so much. i forgot t put the session start thats why i cannot display the username. so in every page you have to session start right?
kv
syntax101 wrote:
so in every page you have to session start right?


Yes otherwise you will not be able to use session variables from previous invocations.

What I suggest you is to have a file, say, authenticate.php, which contains code to authenticate if the user has logged in. It will also contain session_start. ex:

authenticate.php
Code:

<?
session_start();
if($_SESSION['isloggedin']!="true")
{
    header("Location: login.php"); /*assumes login.php is your login page. If not logged in redirect to login page. */
}
?>


Then include this file at the beginning of every php file you write.

Code:

<?
require("authenticate.php");
//rest of your code here. You need not call session_start
?>
syntax101
thanx for the information.
Related topics
.htaccess problem
Account activation, email....
Need help to display Yahoo Online Status Indicator on site
RESOLVED:phpbb prob...soooo close
Database Privilege
phpBB: How to put google ads
javascript/ajax only to have login/user editor
Keyboards Shortcuts
Error I can't seem to fix.
Unable to login
how do i create a Member access page
Any one know the login for...
HELP !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
How to make this code simple?
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.