gpt4 book ai didi

php - 为 pdo php 建立 session

转载 作者:行者123 更新时间:2023-11-29 23:56:16 25 4
gpt4 key购买 nike

基于我的问题here我能够进行 session ,但我没有得到的是,无论我在此处执行什么操作,我都不会直接进入欢迎页面,这是代码。

lock.php

<?php
include('config.php');
session_start();
$user_check=$_SESSION['login_user'];
$stmt = $dbh->prepare("SELECT * FROM member WHERE username = ? ") ;
$stmt->bindValue(1,$user_check);
$stmt->execute();
$selected_row = $stmt->fetch(PDO::FETCH_ASSOC);

$login_session=$selected_row['username'];

if(!isset($login_session))
{
header("Location: login.php");
}
?>

crud.php

<?php 
include_once('config.php');

$error="";

function LoginUser() {
echo "login";
global $dbh;
if(!empty($_POST['un'])){
$username = trim($_POST['un']);
$password = trim($_POST['pw']);
$stmt = $dbh->prepare("SELECT * FROM member WHERE username = ? ") ;
$stmt->bindValue(1,$_POST['un']);
$stmt->execute();
$selected_row = $stmt->fetch(PDO::FETCH_ASSOC);
if($selected_row['username']===$_POST['un']){
if($selected_row['password']===$_POST['pw']){
$_SESSION['username'] = $_POST['un'];
echo $_SESSION['username'];
header("location: welcome.php");
die();
}else{
echo "incorrect password";
header("location: login.php");
die();
}

}else{
echo "user does not exist";
header("location: login.php");
die();
}
}else{
echo "empty";
header("location: login.php");
die();
}
}
function SignUp(){
global $dbh;

if(!empty($_POST['un'])){
$username = trim($_POST['un']);
$password = trim($_POST['pw']);
$stmt = $dbh->prepare("SELECT * FROM member WHERE username = ?") ;
$stmt->bindValue(1,$_POST['un']);
$stmt->execute();
$selected_row = $stmt->fetch(PDO::FETCH_ASSOC);
//if(!empty($stmt->rowCount())){
if($selected_row['username']===$_POST['un']){
//echo "SORRY...YOU ARE ALREADY REGISTERED USER...";
trigger_error("SORRY...YOU ARE ALREADY REGISTERED USER...");
}else{
NewUser();
}
}else{
header("location: signup.php");
die();
}
}

function NewUser(){
global $dbh;
$firstname = trim($_POST['fn']); //at a minimus clear whitespace.
$lastname = trim($_POST['ln']);
$username = trim($_POST['un']);
$password = trim($_POST['pw']);
$address = trim($_POST['cp']);
$stmt = $dbh->prepare("INSERT INTO member (fname,lname,username,password) VALUES (?,?,?,?)");

$stmt->bindValue(1,$firstname,PDO::PARAM_STR);
$stmt->bindValue(2,$lastname,PDO::PARAM_STR);
$stmt->bindValue(3,$username,PDO::PARAM_STR);
$stmt->bindValue(4,$password,PDO::PARAM_STR);
if($stmt->execute()){
echo "YOUR REGISTRATION IS COMPLETED...";
}
}

if (isset($_POST['signup'])) {
//action for update here
SignUp();
}
if (isset($_POST['login'])) {
//action for delete
LoginUser();
}
if (isset($_POST['register'])) {
//action for delete
header("Location: signup.php");
}
if (isset($_POST['back'])) {
//action for delete
header("Location: login.php");
}
?>

登录.php

<?php
include_once('crud.php');
include("config.php");
// Inialize session
session_start();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Login Page</title>

<style type="text/css">
body
{
font-family:Arial, Helvetica, sans-serif;
font-size:14px;

}
label
{
font-weight:bold;

width:100px;
font-size:14px;

}
.box
{
border:#666666 solid 1px;

}
</style>
</head>
<body bgcolor="#FFFFFF">

<div align="center">
<div style="width:300px; border: solid 1px #333333; " align="left">
<div style="background-color:#333333; color:#FFFFFF; padding:3px;"><b>Login</b></div>


<div style="margin:30px">

<form action="crud.php" method="post">
<form method="POST" action="crud.php">
<table border="0">
<form method="POST" action="crud.php">
<tr>
<td>UserName :</td>
<td>
<input type="text" name="un" class="box"/>
</td>
</tr>
<tr>
<td>Password :</td>
<td>
<input type="password" name="pw" class="box" />
</td>
</tr>
<tr>
<td>
<input type="submit" value="Login " name="login"/><br />
</td>
<td>
<input type="submit" value="Register " name="register"/><br />
</td>
</tr>
<tr><td><div style="font-size:11px; color:#cc0000; margin-top:10px"><?php echo $error; ?></div></td></tr>
</form>
</table>
</div>
</div>
</div>

</body>
</html>

欢迎.php

<?php include("config.php");?>
<?php include("lock.php");?>
<?php include_once('crud.php'); ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Welcome </title>
</head>

<body>
<h1>Welcome <?php echo $login_session; ?></h1>

<h2><a href="logout.php">Sign Out</a></h2>
</body>
</html>

从上面的代码中,我想到了一些问题..

  1. 为什么我实际上登录正确,但没有指示欢迎。
  2. 是否有可能lock.php什么也没得到?我的意思是用户名没有值。
  3. 我在本地使用它,但无法在 Chrome 中访问调试,所以我不知道在哪里出错。
  4. 仅供引用,这是教程中的内容。

最佳答案

简单回答:

include_once('crud.php');

lock.php 中,因为它是数据的来源。

关于php - 为 pdo php 建立 session ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25325304/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com