我制作了一个简单的登录和注册系统(http://dak.esy.es/login/),并希望从MySQL数据库的同一行中检索其他信息。目前,只保存用户名(如$_SESSION['username']
),这是在登录页面上输入的内容。在数据库中,我要保存的数据是id
,fullname
和email
- 最好采用与用户名($_SESSION['(name here)']
)相同的格式。我已经有一个文件连接到名为db.php
的数据库。如果有人能提供代码,我会非常高兴。
我login.php
页面上的代码是:
<?php
require('db.php');
session_start();
// If form submitted, insert values into the database.
if (isset($_POST['username'])){
$username = $_POST['username'];
$password = $_POST['password'];
$username = stripslashes($username);
$username = mysql_real_escape_string($username);
$password = stripslashes($password);
$password = mysql_real_escape_string($password);
//Checking is user existing in the database or not
$query = "SELECT * FROM `users` WHERE username='$username' and password='".md5($password)."'";
$result = mysql_query($query) or die(mysql_error());
$rows = mysql_num_rows($result);
if($rows==1){
$_SESSION['username'] = $username;
header("Location: ./index.php"); // Redirect user to index.php
}
else{
echo "<div class='form'><br/><h3>Username or password incorrect.</h3><br/>Click here to <a href='login.php'>try again</a></div>";
}
}
?>
非常感谢。
答案 0 :(得分:0)
目前尚不清楚需求是什么:但我希望,您希望db row中的每个值都作为会话.. 希望您在登录后有会话:`$ _SESSION ['username'];
$conn = mysqli_connect($servername, $username, $password);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$query = "SELECT * FROM `tbl_login` WHERE `username` = ".$_SESSION['username'];
$result = mysqli_query($conn, $sql);
$row = mysqli_fetch_assoc($result);
$_SESSION['id'] = $row["id"];
$_SESSION['name'] = $row["fullname"];
$_SESSION['email'] = $row["email"];
mysqli_close($conn);
或用于设置会话以及登录验证:
$conn = mysqli_connect($servername, $username, $password);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$query = "SELECT * FROM `tbl_login` WHERE `username` = ".$_POST['username']." AND `password`= ".$POST['password'];
$result = mysqli_query($conn, $query);
$row = mysqli_fetch_assoc($result);
if(isset($row)){
$_SESSION['id'] = $row["id"];
$_SESSION['username'] = $row["username"];
$_SESSION['name'] = $row["fullname"];
$_SESSION['email'] = $row["email"];
} else {
redirect('login.php'); //reload login page on failure
}
mysqli_close($conn);
这是基本代码,您可以将此代码拆分为MVC。