i have a login page and it works well. but how can i make like after the log in successfully and all my other pages will have Welcome (lastname) of the user ?

as currently, after log in succesfully will redirect to welcome.php and it seems like it only able to get the email only instead of lastname. and what the correct way to display eg welcome John in all pages.

thanks

login script

<?php 


// Array for recording errors:
$login_errors = array();

// Validate the email address:
if (filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
	$e = mysqli_real_escape_string ($dbc, $_POST['email']);
} else {
	$login_errors['email'] = 'Please enter a valid email address!';
}

// Validate the password:
if (!empty($_POST['pass'])) {
	$p = mysqli_real_escape_string ($dbc, $_POST['pass']);
} else {
	$login_errors['pass'] = 'Please enter your password!';
}
	
if (empty($login_errors)) { // OK to proceed!

	// Query the database:
//	$q = "SELECT email, password FROM members1 WHERE (email='$e' AND password='$p')";		
	$q = "SELECT userid, email, firstname, lastname, password FROM members2 WHERE (email='$e' AND password='$p')";	
		$q2 = "SELECT admin FROM members2 WHERE (admin='1')";	
		

	$r = mysqli_query ($dbc, $q);
			
//$objQuery = mysql_query($q) or die ("Error Query [".$q."]");
	//$r2 = mysqli_query ($dbc, $q2);
	
	if (mysqli_num_rows($r) == 1)   { // A match was made.		
		// Get the data:
		//header('Location: welcome.php');
	
		
		
		$welcomepage = "welcome.php";
		
		echo '<script language="javascript" type="text/javascript">window.location.href="'.$welcomepage.'";</script>';
		
		$row = mysqli_fetch_array ($r, MYSQLI_NUM); 	
		$_SESSION['email'] = $_POST['email'];
	
	} 
	

	
	else { // No match was made.
		$login_errors['login'] = 'The email address and password do not match those on file.';
	}
	
} // End of $login_errors IF.

welcome.php note: part of the code only. (intend to save it as header.php and include it in all other pages)

<div class="mainbar">
      <div class="mainbar_content"> <img class="loginlogo_s" src="styles/Images/loginlogo_s.png" alt="loginlogo" />
        <div class="login"><a href="login-register-page.php" >Logout</a></div>
        <div class="createbar"></div>
    <img class="create_s" src="styles/Images/create_s.png" alt="creates"/>
        <div class="create"><a href="login-register-page.php" >Welcome</a><span class="style2"><?php echo $_SESSION["email"]; ?> </span></div>
      </div>
    </div>

You need to change the session to set to the firstname instead of email or add it as an additional session variable.

You need to change the session to set to the firstname instead of email or add it as an additional session variable.

you mean add something like this below?

$_SESSION = $_POST;

Exactly and then change

<div class="create"><a href="login-register-page.php" >Welcome</a><span class="style2"><?php echo $_SESSION["email"]; ?> </span></div>
      </div>

To

<div class="create"><a href="login-register-page.php" >Welcome</a><span class="style2"><?php echo $_SESSION["lastname"]; ?> </span></div>
      </div>

Exactly and then change

<div class="create"><a href="login-register-page.php" >Welcome</a><span class="style2"><?php echo $_SESSION["email"]; ?> </span></div>
      </div>

To

<div class="create"><a href="login-register-page.php" >Welcome</a><span class="style2"><?php echo $_SESSION["lastname"]; ?> </span></div>
      </div>

i tried before i post here.

welome.php only show Welcome . unable to retrieve lastname with no error message.

Have you got session_start(); at the top of every page? And then have you checked that you have a value in the lastname session? You may want to read my tutorial on how to format and check these things here

Have you got session_start(); at the top of every page? And then have you checked that you have a value in the lastname session? You may want to read my tutorial on how to format and check these things here

yes, session_start(); already at the top of welcome.php

is there anyway to do a simple test to check the value for lastname ?

If you read my tutorial as in the link in my last post it explains how to do it.

If you read my tutorial as in the link in my last post it explains how to do it.

thanks. :)

i have another question.

is there any ways to do like for all my other php pages to check if the user is logged in will show welcome john and a logout link (Session_Destroy() ) but if the user not log in on the page it will show login and create an account link.

if (isset($_SESSION['lastname']) && !empty($_SESSION['lastname'])) {
// Welcome lastname
} else {
// display login
}
if (isset($_SESSION['lastname']) && !empty($_SESSION['lastname'])) {
// Welcome lastname
} else {
// display login
}

thanks for your help =)

for my line 9 , $_SESSION["lastname"]; is this the right ways to display it? because it do not seems to appear.

<? 
     if (isset($_SESSION['lastname']) && !empty($_SESSION['lastname'])) {
     
	 		   echo '
   <div class="login"><a href="login-register-page.php" >Logout</a></div>
   <div class="createbar"></div>
   <img class="create_s" src="styles/Images/create_s.png" alt="creates"/>
   <div class="create"><a href="login-register-page.php" >Welcome</a><span class="style2">';
   $_SESSION["lastname"]; 
echo ' </span></div>';
	   }
		else {
    echo '
	   <div class="login"><a href="login.php" >Login</a></div>
       <div class="createbar"></div> 
       <img class="create_s" src="styles/Images/create_s.png" alt="creates"/>
       <div class="create"><a href="login-register-page.php" >Create an account</a></div>';   
	       }
		?>

Not quite - you have some strange things going on with your echo and can I also suggest that you always ope your PHP tags with <?php, not short tags. I have re-jigged things for you to sort it out and make it cleaner and easier for you to understand (I hope ;) )

<?php 
     if (isset($_SESSION['lastname']) && !empty($_SESSION['lastname'])) {
?>     
   <div class="login"><a href="login-register-page.php" >Logout</a></div>
   <div class="createbar"></div>
   <img class="create_s" src="styles/Images/create_s.png" alt="creates"/>
   <div class="create"><a href="login-register-page.php" >Welcome</a><span class="style2">
   <?php echo $_SESSION["lastname"]; ?>
		</span></div>
<?php	} else { ?>
	   <div class="login"><a href="login.php" >Login</a></div>
       <div class="createbar"></div> 
       <img class="create_s" src="styles/Images/create_s.png" alt="creates"/>
       <div class="create"><a href="login-register-page.php" >Create an account</a></div>
<?php } ?>

thanks it works! :)

Great :) Can you mark the thread as solved please?

oeps!

commented: What is that supposed to mean -1
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.