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
Are you not using sessions? And/or cookies?
They allow you to pass variables around your various pages on a per user basis.
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
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()
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.
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?
| 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
?>
|
thanx for the information.