I'm having issues with a simple login system that I'm trying to create. Here is my setup:
index.php
<?php if(!isset($_SESSION['username'])){
session_start();
//present login form with action=login.php
} else {
//display actual page
}
?>
login.php
<?php
if(!empty($_SESSION['username'])){
echo $_SESSION['username'];
} else {
$username=$_POST['username'];
$password=sha1($_POST['password']);
$link=mysqli_connect("localhost","root","password","database",3306,"/tmp/mysql.sock");
$query="SELECT * FROM login WHERE `name`='".$username."' AND `passwordhash`='".$password."' LIMIT 1";
$result=mysqli_query($link,$query);
if(mysqli_num_rows($result)==1){
session_start();
$_SESSION['username']=$username;
$row=mysqli_fetch_assoc($result);
$_SESSION['firstname']=$row['firstname'];
$_SESSION['lastname']=$row['lastname'];
echo "Username: ". $_SESSION['username']." Name: ".$_SESSION['firstname']." ".$_SESSION['lastname']; //this is just here as POC. If I redirect to index.php (using header()), it shows the login form (claims $_SESSION['username'] is unset).
}
}
?>
Once I log in, it shows my username and first and last names just fine on login.php (not a database problem). But if I redirect to index.php, it shows the login form immediately. What's the issue? EDIT: Even after adding session_start() to login.php, the same outcome results.
使session_start()成为文件的第一行,它必须包含在使用会话变量的所有文件中
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.