April 20, 2012
I want to display the online user when they have logged on
Question by blanksby
I would like to display:
Logged in as:_______
The code so far:
The login check,
<?php
include('config.php');
// username and password sent from form
$myemail=$_POST['myemail'];
$mypassword=$_POST['mypassword'];
// To protect MySQL injection
$myemail = stripslashes($myemail);
$mypassword = stripslashes($mypassword);
$myemail = mysql_real_escape_string($myemail);
$mypassword = mysql_real_escape_string($mypassword);
$sql="SELECT * FROM $tbl_name WHERE email='$myemail' and password='$mypassword'";
$result=mysql_query($sql);
// Mysql_num_row is counting table row
$count=mysql_num_rows($result);
if($count==1){
session_start();
$_SESSION['myemail'] = $myemail;
header("Location: http://www.jblanksby.yourwebsolution.net/login_success.php? user=$myemail"); }
else
{ header("Location: http://www.jblanksby.yourwebsolution.net/loginerror.php");
}
?>
The login success page/first members page,
<?
$email = $_GET['myemail'];
session_start();
$_SESSION['myemail'] = $email;
if(isset($_SESSION['email'])){
} else {
echo "
<script language='javascript'>
alert('Sorry, but you must login to view the members area!')
</script>
<script>
window.location='http://jblanksby.yourwebsolution.net/sign_in.php'
</script>
"; }
?>
<html>
blah blah blah
</html>
The code used to display the users email,
Logged in as: <? echo "$email"; ?>
The log in side of things is perfect. Just displaying the users email is proving difficult. What have I done wrong/missed out?
Answer by Starx
Since you are trying to display values out from the session, you have to use $_SESSION['email']
.
session_start(); // Or, if you have done it ahead, then omit this
echo "Logged in as:".$_SESSION['email'];