Php 会话_start()工作不正常
我希望你做得很好, 我用登录帐户创建这个网站显然,我已经在本地数据库和服务器数据库中注册了我的用户。我的HTML/PHP代码在运行时没有显示任何错误。我已经检查了数据库连接。没错。该网站允许我使用任何用户和密码登录。它似乎无法正确验证我输入的数据。尽管我检查了我的SQL命令。 我想知道你是否能帮我对付这些家伙。你是最棒的!:) 谢谢,干杯 下面是我的一段有用的代码: My header-header.php:Php 会话_start()工作不正常,php,html,Php,Html,我希望你做得很好, 我用登录帐户创建这个网站显然,我已经在本地数据库和服务器数据库中注册了我的用户。我的HTML/PHP代码在运行时没有显示任何错误。我已经检查了数据库连接。没错。该网站允许我使用任何用户和密码登录。它似乎无法正确验证我输入的数据。尽管我检查了我的SQL命令。 我想知道你是否能帮我对付这些家伙。你是最棒的!:) 谢谢,干杯 下面是我的一段有用的代码: My header-header.php: <html> <?php /* static $called =
<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>';
}
?>
进展-反馈:
好了,伙计们,我试过你让我做的。它开始让我自动登录。会话['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';
?>
请参阅手册的注释部分。修改代码如下:
<?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\u start()放在页面顶部?session\u start
必须在任何输出之前(包括
标记之前)@cale\u b top-否,在输出之前-yes@dagon-技术性,但正确。井编程是技术性的:-)OP从未接受过答案,但你可能会很幸运:——)@Dagon我们可以教他如何接受。他可能没有意识到这一点。我从他的反驳中猜出来