gpt4 book ai didi

php - session_start() 无法正常工作

转载 作者:太空宇宙 更新时间:2023-11-04 16:08:42 24 4
gpt4 key购买 nike

我希望你做得很好,我显然正在用登录帐户制作这个网站,我已经在本地数据库和服务器数据库中注册了我的用户。我的 HTML/PHP 代码在运行时没有显示任何错误。我已经检查了我的数据库连接。这是正确的。该网站允许我使用任何用户名和密码登录。它似乎没有正确验证我输入的数据。虽然我检查了我的 SQL 命令。我想知道你是否可以帮助我解决这些问题。你是最棒的! :)提前致谢,干杯这是我的一段有用的代码:我的标题 - Header.php:

<html>

<?php
/* static $called = FALSE;
if (!$called)
{*/
session_start();
/*$called = true;
}*/

include_once 'debugging.php';
?>
<head>
<link href="style.css" rel="stylesheet" type="text/css" />
</head>

<body>
<div >
<dt id="navbar1" class ="navbar">
<a href="Index.php" class="followingLink">Home</a>
<a href="Upload.php">Upload Videos</a>
</dt>
</div>
<?php
if (isset($_SESSION['logged'])) {
echo '<div class="right navbar" id = "navbar2">
<a href="Index.php" class = "right followingLink1">Log out</a>
<p class = "right">/</p>
<a href="Edit_Account.php" class = "right">Edit Account</a>
<img src="http://www.extremetech.com/wp-content/uploads/2013/11/emp-blast.jpg?type=square"
height="42" width="42" class = "right"/>
</div>';
} else {
echo '<div class="right navbar" id = "navbar2">
<a href="Login.php" class = "right">Login</a>
<p class = "right">/</p>
<a href="Sign_Up.php" class = "right">Sign Up</a>
</div>';
}
?>

进度-反馈:

好的,伙计们,我试过你让我做的事了。它开始让我自动登录。可能 session['logged'] 变量声明被认为是真实的。只有当用户从登录页面登录时,我才将其设置为 true。但它不是那样运作的。这是我的登录页面代码:

<?php
include_once 'Header.php';
?>

<div id="container">
<br>


<?php
/*
if($_DEBUG)
{
ini_set('display_errors', 1);
ini_set('log_errors', 1);
ini_set('error_log', dirname(__FILE__) . '/error_log.txt');
error_reporting(E_ALL);
}


$page_title = 'Login';/* */

//in this page we do things slightly differently - the code for validation
and displaying messages is done
//before we display the form
echo '<div id = "div_1"><h1>Login</h1>';

//display the form
echo '<div id="div_2"><div id="div_2">
<form action="index.php" method="post">
<label>UserName<br>
<span class="small">enter your username</span>
</label>
<input type="text" name="UserName" value=""/>

<label><br>Password<br>
<span class="small">enter your password</span>
</label>
<input type="password" name="Password" />

<button type="submit" name="submit" value="Login" />Log in</button>
<input type ="hidden" name="submitted" value="TRUE">
</form>
</div>
</div>';

if (isset($_POST['submitted'])) {
//require_once is similar to 'include' but ensures the code is not
copied multiple times
require_once('LoginFunctions.php');

//list() is a way of assigning multiple values at the same time
//checkLogin() function returns an array so list here assigns the
values in the array to $check and $data
list($check, $data) = checkLogin($_POST['UserName'],
$_POST['Password']);

if ($check) {
setcookie('FName', $data['FName'], time()+ 900 ) ; //cookie
expires after 15 mins
setcookie('LName', $data['LName'], time() + 900 ) ;
//
//use session variables instead of cookies
//these variables should now be available to all pages in the
application as long as the users session exists
$_SESSION['FName'] = $data['FName'];
$_SESSION['LName'] = $data['LName'];
$_SESSION['UserName'] = $data['UserName'];
//to enable $_SESSION array to be populated we always need to call
start_session() - this is done in header.php
//print_r is will print out the contents of an array
print_r($_SESSION);
//
//Redirect to another page
$url = absolute_url('Index.php'); //function defined in
Loginfunctions.php to give absolute path for required page
$_SESSION['logged'] = TRUE;
//this version of the header function is used to redirect to
another page
header("Location: $url");//since we have entered correct login
details we are now being directed to the home page

exit();
} else {
$errors = $data;
}
}

//create a sopace between the button and the error messages
//echo'<div class="spacer"></div>';

if (!empty($errors)) {
echo '<br/> <p class="error">The following errors occurred: <br
/>';

//foreach is a simplified version of the 'for' loop
foreach ($errors as $err) {
echo "$err <br />";
}

echo '</p>';
}

//this is the end of the <div> that contains the form
echo '</div>';

/* */
?>
</div>

<?php
include 'Footer.php';
?>

最佳答案

请参阅 session_start documentation注释部分.修改您的代码如下:

<?php 
// Start the session before ANY output.
// Start the session always - there's little / no value to only starting sometimes.
session_start(); ?>
<html>

<?php
/* static $called = FALSE;
if (!$called)
{*/

/*$called = true;
}*/

include_once 'debugging.php';
?>
<head>

session_start 必须在任何输出发送到浏览器 之前运行。此外,将它放在 if 语句中没有任何值(value),因此请保持简单并将其放在任何输出之前始终运行的位置。

关于php - session_start() 无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36070618/

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