创建会话用户登录php [英] Creating a session user login php
本文介绍了创建会话用户登录php的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我被困在如何为登录用户创建会话.我要进行部分检查以确保登录信息与数据库信息相对应,但是被困在如何获取电子邮件地址和存储到会话中.这是下面的我的PHP代码.
I'm stuck on how to create a session for a user who logs in. I got the part of checking to make sure the log in information corresponds with the database information, but is stuck on how to take the email address and store into a session. Here is my php code below.
<?php include '../View/header.php';
session_start();
require('../model/database.php');
$email = $_POST['username'];
$password = $_POST['password'];
$sql = "SELECT emailAddress FROM customers WHERE emailAddress ='$email' AND password = '$password'";
$result = mysql_query($sql, $db);
if (!$result) {
echo "DB Error, could not query the database\n";
echo 'MySQL Error: ' . mysql_error();
exit;
}
while ($row = mysql_fetch_assoc($result)) {
echo $row['emailAddress'];
}
mysql_free_result($result);
?>
推荐答案
尝试以下操作.
请注意,此mysql连接类型已被弃用,请查找PDO.另外,您还需要阅读有关sql注入的内容,因为这是不安全的.
Please note that this mysql connection type has been depreciated, look up PDO. Also you need to read up on sql injection as this is not safe.
<?php
//always put session_start() at the top of the file
session_start();
include('../View/header.php');
require ('../model/database.php');
$email = $_POST['username'];
$password = $_POST['password'];
$sql = "SELECT emailAddress FROM customers WHERE emailAddress = '$email' AND password = '$password'";
$result = mysql_query($sql, $db);
if(!$result){
echo "DB Error, could not query the database\n";
echo 'MySQL Error: ' . mysql_error();
exit;
}
while($row = mysql_fetch_assoc($result)) {
echo $row['emailAddress'];
$_SESSION['email'] = $row['emailAddress'];
$_SESSION['batmansName'] = "Bruce Wayne";
}
mysql_free_result($result);
?>`
<a href="anotherFile.php">Click to output the contents of our session :)</a>
anotherFile.php
anotherFile.php
<?php
//always put session_start() at the top of the file
session_start();
//print out everything that we have stored in the session
print_r($_SESSION);
echo "<br /><br />";
echo "Session email: ".$_SESSION['email']."<br />";
echo "Batman's real name: ".$_SESSION['batmansName']."<br />";
?>
这篇关于创建会话用户登录php的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文