我有两个名为dblogin的数据库,其中存储了所有注册详细信息,以及表单(在用户主页上)详细信息的其他信息。现在我使用session在dblogin中显示与用户相关的数据,但是如何使用会话ID显示用户在主页上填写的数据? 想根据用户会话ID在view.php上查看home.php上的表单详细信息。
class.user.php
<?php
require_once('dbconfig.php');``
class USER
{
private $conn;
public function __construct()
{
$database = new Database();
$db = $database->dbConnection();
$this->conn = $db;
}
public function runQuery($sql)
{
$stmt = $this->conn->prepare($sql);
return $stmt;
}
public function register($uname,$umail,$upass)
{
try
{
$new_password = password_hash($upass, PASSWORD_DEFAULT);
$stmt = $this->conn->prepare("INSERT INTO users(user_name,user_email,user_pass)VALUES(:uname, :umail, :upass)");
$stmt->bindparam(":uname", $uname);
$stmt->bindparam(":umail", $umail);
$stmt->bindparam(":upass", $new_password);
$stmt->execute();
return $stmt;
}
catch(PDOException $e)
{
echo $e->getMessage();
}
}
public function doLogin($uname,$umail,$upass)
{
try
{
$stmt = $this->conn->prepare("SELECT user_id, user_name, user_email, user_pass FROM users WHERE user_name=:uname OR user_email=:umail ");
$stmt->execute(array(':uname'=>$uname, ':umail'=>$umail));
$userRow=$stmt->fetch(PDO::FETCH_ASSOC);
if($stmt->rowCount() == 1)
{
if(password_verify($upass, $userRow['user_pass']))
{
$_SESSION['user_session'] = $userRow['user_id'];
return true;
}
else
{
return false;
}
}
}
catch(PDOException $e)
{
echo $e->getMessage();
}
}
public function is_loggedin()
{
if(isset($_SESSION['user_session']))
{
return true;
}
}
public function redirect($url)
{
header("Location: $url");
}
public function doLogout()
{
session_destroy();
unset($_SESSION['user_session']);
return true;
}
}
?>
dbconfig.php
<?php
class Database
{
private $host = "localhost";
private $db_name = "dblogin";
private $username = "root";
private $password = "";
public $conn;
public function dbConnection()
{
$this->conn = null;
try
{
$this->conn = new PDO("mysql:host=" . $this->host . ";dbname=" . $this->db_name, $this->username, $this->password);
$this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $exception)
{
echo "Connection error: " . $exception->getMessage();
}
return $this->conn;
}
}
?>
home.php
<?php
require_once("session.php");
require_once("class.user.php");
$auth_user = new USER();
$user_id = $_SESSION['user_session'];
$stmt = $auth_user->runQuery("SELECT * FROM users WHERE user_id=:user_id");
$stmt->execute(array(":user_id"=>$user_id));
$userRow=$stmt->fetch(PDO::FETCH_ASSOC);
?>
<nav class="navbar navbar-default navbar-fixed-top">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
</div>
<div id="navbar" class="navbar-collapse collapse">
<ul class="nav navbar-nav navbar-right">
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">
<span class="glyphicon glyphicon-user"></span> Hi' <?php echo $userRow['user_name']; ?> <span class="caret"></span></a>
<ul class="dropdown-menu">
<li><a href="profile.php"><span class="glyphicon glyphicon-user"></span> View Profile</a></li>
<li><a href="logout.php?logout=true"><span class="glyphicon glyphicon-log-out"></span> Sign Out</a></li>
</ul>
</li>
</ul>
</div><!--/.nav-collapse -->
</div>
</nav>
<div class="clearfix"></div>
<div class="container-fluid" style="margin-top:80px;">
<div class="container">
<label class="h5">welcome : <?php print($userRow['user_name']); ?></label>
<h1>
<a href="home.php"><span class="glyphicon glyphicon-home"></span> home</a>
<a href="profile.php"><span class="glyphicon glyphicon-user"></span> profile</a></h1>
<hr />
<div class="navbar-header">
<a class="navbar-brand" href="#">The-Echeck</a>
</div>
<ul class="nav navbar-nav">
<li class="active"><a href="home.php">Home</a></li>
<li><a href="view.php">View/Print Check</a></li>
<li><a href="#">Export as CSV</a></li>
</ul>
<form action="insert.php" method="post">
<p id="emp"><input type="text" name="e_name" id="e_name" placeholder="Payee Name" required style='text-transform:uppercase'></p>
<p><input type="text" name="check_no" id="check_no" placeholder="Check Number" required style='text-transform:uppercase'></p>
<p><input type="text" name="amount" id="amount" placeholder="Amount" required style='text-transform:uppercase'></p>
<!--<p><input type="text" name="amu" id="amu" placeholder="Amount In Words" required></p>-->
<p><input type="text" name="routing" id="routing" placeholder="Routing #" required style='text-transform:uppercase'></p>
<p><input type="text" name="ch_acc" id="ch_acc" placeholder="Checking Account #" required style='text-transform:uppercase'></p>
<p><input type="text" name="con_acc" id="con_acc" placeholder="Confirm Account #" required style='text-transform:uppercase'> </p>
<p><input type="Date" name="date" id="date" placeholder="Date" required style='text-transform:uppercase'></p>
<p><input type="text" name="p_name" id="p_name" placeholder="Payor Name" required style='text-transform:uppercase'></p>
<p><input type="text" name="addr" id="addr" placeholder="Address" required style='text-transform:uppercase'></p>
<p><input type="text" name="city" id="city" placeholder="City" required style='text-transform:uppercase'></p>
<p><input type="text" name="state" id="state" placeholder="State" required style='text-transform:uppercase'></p>
<p><input type="text" name="zip" id="zip" placeholder="Zip" required style='text-transform:uppercase'></p>
<p><input type="text" name="phone" id="phone" placeholder="Phone" required style='text-transform:uppercase'></p>
<p><input type="text" name="memo1" id="memo1" placeholder="Memo1" required</p>
<p><input type="text" name="memo2" id="memo2" placeholder="Memo2" required</p>
<input type="submit" Value="Submit" name="submit" id="submit">
</form>
</div>
view.php
<?php
require_once("session.php");
require_once("db_details.php");
require_once("class.user.php");
$auth_user = new USER();
$user_id = $_SESSION['user_session'];
$stmt = $auth_user->runQuery("SELECT * FROM users WHERE user_id=:user_id");
$stmt->execute(array(":user_id"=>$user_id));
$userRow=$stmt->fetch(PDO::FETCH_ASSOC);
?>
<nav class="navbar navbar-default navbar-fixed-top">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
</div>
<div id="navbar" class="navbar-collapse collapse">
<ul class="nav navbar-nav navbar-right">
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">
<span class="glyphicon glyphicon-user"></span> Hi <?php echo $userRow['user_name']; ?> <span class="caret"></span></a>
<ul class="dropdown-menu">
<li><a href="profile.php"><span class="glyphicon glyphicon-user"></span> View Profile</a></li>
<li><a href="logout.php?logout=true"><span class="glyphicon glyphicon-log-out"></span> Sign Out</a></li>
</ul>
</li>
</ul>
</div><!--/.nav-collapse -->
</div>
</nav>
<div class="clearfix"></div>
<div class="container-fluid" style="margin-top:80px;">
<div class="container">
<label class="h5">welcome : <?php print($userRow['user_name']); ?></label>
<div class="navbar-header">
<a class="navbar-brand" href="#">The-Echeck</a>
</div>
<ul class="nav navbar-nav">
<li class="active"><a href="home.php">Home</a></li>
<li><a href="view.php">View/Print Check</a></li>
<li><a href="#">Export as CSV</a></li>
</ul>
<br>
<br>
<br>
<?php
$query="SELECT * FROM details WHERE";
try
{
$stmt = $db->prepare($query);
$stmt->execute();
}
catch(PDOException $ex)
{
die("Failed to run query: " . $ex->getMessage());
}
$rows = $stmt->fetchAll();
?>
<h1>Memberlist</h1>
<table border='1'>
<tr>
<th>Date</th>
<th>Name</th>
<th>Amount</th>
</tr>
<?php foreach($rows as $row): ?>
<tr>
<td><?php echo htmlentities($row['date'], ENT_QUOTES, 'UTF-8'); ?></td>
<td><?php echo htmlentities($row['name'], ENT_QUOTES, 'UTF-8'); ?></td>
<td><?php echo htmlentities($row['amount'], ENT_QUOTES, 'UTF-8'); ?></td>
</tr>
<?php endforeach; ?>
db_details.php
<?php
// These variables define the connection information for your MySQL database
$username = "root";
$password = "";
$host = "localhost";
$dbname = "echeck";
$options = array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8');
try
{
$db = new PDO("mysql:host={$host};dbname={$dbname};charset=utf8", $username, $password, $options);
}
catch(PDOException $ex)
{
die("Failed to connect to the database: " . $ex->getMessage());
}
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
?>
答案 0 :(得分:0)
创建此php文件以在登录期间验证用户身份。我称之为validate.php
<?php
// Start the session
session_start();
?>
<?php
include('config.php');//this is the database connection
{
$username=$_POST['username'];//values picked from login form
$password=$_POST['password'];//values picked from login form
}
$sql = "SELECT * FROM users_tbl WHERE username ='$name' AND password='$password'";
$result = mysqli_query($con,$sql);
$row = mysqli_num_rows($result);
if($row==1)
{
$_SESSION["username"] = "$session_id";
header("location:homepage.php");
}
else{
echo "Either your password is wrong or username. Please Try again</br>";
}
?>
在您的homepage.html登录成功后,试试这个
<?php
session_start();
if (isset($_SESSION['username'])) {
$session_id=$_SESSION["username"];
}
?>
<html>
<body>
<?php
include(config.php);//database connection
$session_id=$_SESSION["username"]; // we pick the value stored in our session
$sql = "SELECT * FROM users_tbl WHERE username='$session_id'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "<br>";
}
} else {
echo "Something went wrong";
}
$con->close();
?>
</body>
</html>
数据库连接config.php
<?php
$host='localhost';
$user='root';
$pass='';
$db='vet';//database name
$con= mysqli_connect($host,$user,$pass,$db);
if (!$con) {
die("Connection failed: " . mysqli_connect_error());
}
?>