Jump to content

Display Username after login


Arcadia

Recommended Posts

Hi everyone i'm having a problem with my login system and really hope someone can help me out here..

 

I made a login page and it's all working and it perfectly connect to my database but I also want the option when someone logins that they see a welcome message on each page on the website like "Welcome, $username"

 

 

I searched on google but each time i pumb into errors :(

 

I would like to show the "welcome message" in the header.

 

 

 

Below you can see my code:

 

Home.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link rel="stylesheet" type="text/css" href="Design.css">
<title>Fotoalbum</title>


<script src="http://maps.googleapis.com/maps/api/js"></script>
<script>
function initialize() {
  var mapProp = {
    center:new google.maps.LatLng(51.508742,-0.120850),
    zoom:5,
    mapTypeId:google.maps.MapTypeId.ROADMAP
  };
  var map=new google.maps.Map(document.getElementById("googleMap"),mapProp);
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>


</head>

<body>

<div id ="Wrapper">


<div id ="Logo">

<img src="Afbeeldingen/10881558_759342210782402_1567173209848574625_n.jpg" width="160" height="150" />

</div>

<div id ="Header">


<center>
<p> </p>

<H1><p>CB </p></H1>
</center>
<a href="Main_login.php">Login</a>
<a href="Logout.php">Logout</a>

<ul>
  <li><a href="#">Home</a></li>
<li><a href="#">Biografie</a></li>
<li><a href="#">Fotoalbum</a></li>
<li><a href="#">Contact</a></li>


</ul>
</center>

</div>
</body>
</html>

Main_LoginForm.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Main Login</title>
</head>

<body>


<form action="Inlog.php" method="post">
<label>UserName :</label>
<input type="text" name="Naam"/><br />
<label>Password :</label>
<input type="password" name="Wachtwoord"/><br/>
<p>
<input type="submit" value=" Submit "/><br />
</form>


</body>
</html>

Inlog.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
</head>

<body>

<?php

ob_start();
$host="localhost"; // Host name
$username="root"; // Mysql username
$password=""; // Mysql password
$db_name="InlogFA"; // Database name
$tbl_name="InlogFotoalbum"; // Table name

// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

// Define $myusername and $mypassword
$myusername=$_POST['Naam'];
$mypassword=$_POST['Wachtwoord'];

// To protect MySQL injection (more detail about MySQL injection)
$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);
$myusername = mysql_real_escape_string($myusername);
$mypassword = mysql_real_escape_string($mypassword);

$sql="SELECT * FROM $tbl_name WHERE Naam='$myusername' and Wachtwoord='$mypassword'";
$result=mysql_query($sql);

// Mysql_num_row is counting table row
$count=mysql_num_rows($result);

// If result matched $myusername and $mypassword, table row must be 1 row

if($count==1){

// Register $myusername, $mypassword and redirect to file "login_success.php"

$_SESSION['myusername']= "myusername";
$_SESSION['mypassword']= "mypassword";
 header("Location: Login_success.php");
 

}
else {
echo "Wrong Username or Password";
}

ob_end_flush();
?>
</body>
</html>

Login_succesful.php

<?php

session_start();

if(!$_SESSION['myusername']){
    header("Location: Fotoalbum.php");

}

?>


<body>
Login Succesful!
</body>


</html>
Link to comment
https://forums.phpfreaks.com/topic/295179-display-username-after-login/
Share on other sites

If I see this right, you're not starting a session until the user gets to Login_successful.php, which comes after the user logs in on Inlog.php, where you're trying to set $_SESSION variables

$_SESSION['myusername']= "myusername";
$_SESSION['mypassword']= "mypassword";
header("Location: Login_success.php");

You have to start your session on the Inlog.php (if not sooner) to be able to set your SESSION variables. I.E. - start_session() has to be called before you can set $_SESSION['variables'].

 

Also, your header has them being directed to Login_success.php, yet the page you gave us in called Login_successful.php. Change that, start your session on the login page so that your $_SESSION vars carry from one page to the next. Also, as you move from one page to the next, be sure to set those $_SESSION vars:

 

SomePageAfterInlog.php

$myun = $_SESSION['myusername'];
$mypw = $_SESSION['mypassword'];

Then use those variables ($myun AND $mypw) on that page. And on any pages the user goes after logging in, be sure to set your $_SESSION vars like I just showed you. Every page you want to use them has to have them.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.