gpt4 book ai didi

php - session 的乐趣

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

我有一个问题很困扰我。这是因为我正在尝试制作一个 PHP 登录脚本。但是当我正确登录时,它不会让我进入。

如果我注释掉一些行(我会标记它们),脚本就可以工作,但那是我计划用来在每个页面上签到的代码,所以如果人们不这样做,他们就不能进来应该有访问权限。

下面,我已经发布了代码。


索引.php

<?php
function __autoload($class_name) {
require_once "./functions/" . $class_name . ".php";
}
$functions = new functions;
$functions->header("Log ind",0);
$login = new login;
$login->showLogin();
$functions->footer();
?>

/functions/functions.php

<?php
// Define class functions
class functions {
function header($titel,$needlogin = 1) {
session_start();
echo $_SESSION['navn'];
// The following lines can be commented out, and it's working
if($needlogin == 1) {
if(!isset($_SESSION['id'])) {
header("Location: http://hansensopskrifter.co.cc/");
exit;
}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" >
<head>
<title><?php echo $titel; ?> - Hansens Opskrifter</title>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
</head>
<body>
<h1>Hansens Opskrifter</h1>
<?php
// The above lines can be commented out, and it's working
}

function footer() {
?>
</body>
</html>
<?php
}
}
?>

/functions/mysql.php

<?php
// Create the class MySQL
class mysql {
function __construct() {
$this->mysqlconnect();
}

function mysqlconnect() {
$conn = mysql_connect("localhost","user","pass");
if(!$conn) {
die("Noget gik galt - kontakt Kristoffer og vis ham den følgende meddelelse: " . mysql_error());
}
if(!mysql_select_db("db",$conn)) {
die("Noget gik galt - kontakt Kristoffer og vis ham den følgende meddelelse: " . mysql_error());
}
}

function mysqlquery($query) {
$result = mysql_query($query);
if($result) {
return $result;
}
}
}
?>

登录.php

<?php
function __autoload($class_name) {
require_once "./functions/" . $class_name . ".php";
}
$name = $_REQUEST['name'];
$pass = $_REQUEST['pass'];
$login = new login;
$l = $login->doLogin($name,$pass);
if($l == TRUE) {
header("Location: http://hansensopskrifter.co.cc/loggedin.php");
} else {
exit;
}
?>

/functions/login.php

<?php
class login {
function __autoload($class_name) {
require_once($class_name . ".php");
}
function showLogin() {
?>
<h2>Log ind</h2>
<form action="./login.php" method="post">
Navn:<input type="text" name="name" />
Kode:<input type="password" name="pass" />
<input type="submit" value="Log ind" />
</form>
<p><a href="./forgotpass.php" alt="Glemt kode" title="Glemt kode">Glemt kode?</a></p>
<?php
}

function doLogin($name,$pass) {
$mysql = new mysql;
$n = mysql_real_escape_string($name);
if(!$n) {
$functions = new functions;
$functions->header("Intet navn indtastet",0);
echo "Du glemte at indtaste dit navn.";
$this->showLogin();
$functions->footer();
return false;
} elseif(!$pass) {
$functions = new functions;
$functions->header("Ingen adgangskode indtastet",0);
echo "Du glemte at indtaste din adgangskode.";
$this->showLogin();
$functions->footer();
}
$query = "SELECT `id`, `navn`, `kode` FROM `users` WHERE `navn` = '".$n."' ";
$result = $mysql->mysqlquery($query);
while($row = mysql_fetch_assoc($result)) {
$k = sha1($pass);
$navn = $row['navn'];
$kode = $row['kode'];
$n = ucfirst(strtolower($n));
if($navn == $n && $kode == $k) {
$_SESSION['id'] = $row['id'];
$_SESSION['navn'] = $row['navn'];
return true;
} else {
$functions = new functions;
$functions->header("Forkert navn eller kode",0);
echo "Det indtastede navn eller kode er forkert.";
$this->showLogin();
$functions->footer();
return false;
}
}
}
}
?>

登录.php

<?php
function __autoload($class_name) {
require_once ("./functions/" . $class_name . ".php");
}
header( "refresh:2;url=./panel/index.php",0);
$functions = new functions;
$functions->header("Logger ind...");
?>
<p>Du er nu logget ind. Du vil automatisk blive viderestillet om omkring 5 sekunder. Hvis du er tr&aelig;t af at vente kan du <a href="./panel/index.php" alt="Opskrifter" title="Opskrifter">klikke her</a>.</p>
<?php
$functions->footer();
?>

我已经尝试了很多东西,现在,我只希望你能帮助我。我检查过在我的计算机上创建了一个名为 PHPSESSID 的 cookie。

非常感谢您。

最佳答案

那是一堵巨大的文字墙,但在您的 index.php 中似乎存在缺陷:

$login = new login;
$login->showLogin();

您无条件地创建一个登录对象并显示登录表单,而无需检查先前的登录尝试是否成功。不知道你在登录类中设置的$_SESSION['id']$_SESSION['navn']是不是代表登录成功,但假设它们是,你应该有类似的东西:

if (isset($_SESSION['id'])) {
// not logged in, show the form
$login = new login;
$login->showLogin();
exit();
}
// show logged in content here

关于php - session 的乐趣,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3401219/

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