Edit
In your index.php
file:
Try putting session_start(); $_SESSION['name'] = $_POST['name'];
between $membre = $stmt->fetch(PDO::FETCH_ASSOC);
and header("location:test.php");
then follow the conditional statements I put in my original answer.
This I tested and worked for me:
...
if($nbMembre == 1){
$membre = $stmt->fetch(PDO::FETCH_ASSOC);
// my addition
session_start();
$_SESSION['name'] = $_POST['name'];
header("location:test.php");
} else {
$msg="<font color='orange'>Enter username and password.</font>";
}
} else {
$msg ="<font color='red'>Lgon!</font>";
}
...
// rest of code
Original answer
Base yourself on the following: (test code works as one file)
Assign a variable to the form's POST element, then assign a session name from the $_POST
.
This would be considered as your login page:
<?php
session_start();
// require 'connect.php';
if(isset($_POST['submit'])){
$name = $_POST['name'];
$_SESSION['name'] = $_POST['name'];
if(isset($_SESSION['name']) && !empty($_SESSION['name'])){
echo "session is set";
echo "<br>";
echo $name;
} else {
echo "session is NOT set";
}
} // brace for if(isset($_POST['submit']))
?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Test</title>
</head>
<body>
<form action="" method="post">
<label for="name">Username: </label>
<input type="text" name="name" /><br/>
<label for="password">Password: </label>
<input type="password" name="password" /><br/>
<input type="submit" name="submit" value="TEST" />
</form>
</body>
</html>
Then this, your test.php as you have it now.
<?php
session_start();
require 'connect.php';
if(isset($_SESSION['name']) && !empty($_SESSION['name'])){
echo "session is set";
echo "<br>";
echo $name;
} else {
// echo "session is NOT set";
header("location:index.php");
}
/* old method
if(!isset($_SESSION['name'])){
header("location:index.php");
exit();
}
*/
?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Test</title>
</head>
<body>
<header>
</div>
wellcome
Hellos-.....
<br>
<a href="logout.php"><h2>Logout</h2></a>
</div>
</body>
</html>
Passwords:
I also noticed that you are storing passwords using MD5. This is no longer recommended.
Here are a few password storage options:
crypt()
bcrypt()
scrypt()
- PBKDF2
- PBKDF2 on PHP.net
- PHP 5.5's
password_hash()
function.
Other links:
Sessions:
Here is an intuitive tutorial on sessions: