我正在从Lynda.com关注一个php视频教程(它不在线抱歉)并使用以下代码,但我收到以下错误
Error 310 (net::ERR_TOO_MANY_REDIRECTS): There were too many redirects.
这可能是我的代码的问题。即代码在前10行或15行中有两个redirect_to
的事实,还是在讨论别的东西?
<?php require_once("../../includes/initialize.php"); ?>
<? if(!$session->is_logged_in()){
redirect_to("login.php"); } ?>
<?php
$logfile = SITE_ROOT.DS.'logs'.DS.'log.txt';
if($_GET['clear'] == 'true') {
file_put_contents($logfile, '');
//add the first log entry
log_action('Logs Cleared', "by User ID {$session->user_id}");
//redirect to this same page so that the URL won't
//have "clear=true" anymore
redirect_to('logfile.php');
}
?>
<?php include_layout_templates('admin_header.php');?>
<a href="index.php">« Back</a><br/>
<br/>
<h2>Log File</h2>
<p><a href="logfile.php?clear=true">Clear log file</a></p>
<?php
if (file_exists($logfile) && is_readable($logfile) &&
$handle = fopen($logfile, 'r')) {//read
echo "<ul class=\"logentries\">";
while(!feof($handle)) {
$entry = fgets($handle);
if(trim($entry) != "") {
echo "<li>{$entry}</li>";
}
}
echo "</ul>";
fclose($handle);
} else {
echo "Could not read from {$logfile}.";
}
?>
//Remember to give your form's submit tag a name="submit" attribute
if (isset($_POST['submit'])) {//Form has been submitted.
$username = trim($_POST['username']);
$password = trim($_POST['password']);
//Check database to see if username/password exist
$found_user = User::authenticate($username, $password);
if ($found_user) {
$session->login($found_user);
log_action('Login', "{$found_user->username} loggined in.");
redirect_to("index.php");
} else {
//username/password combo was not found in the database
$message = "Username/password combination incorrect.";
}
} else {//Form has not been submitted.
$username = "";
$password = "";
}
?>
<?php include_layout_template('admin_header.php'); ?>
<h2>Staff Login</h2>
<?php echo output_message($message); ?>
<form action="login.php" method="post">
<table>
<tr>
<td>Username:</td>
<td>
<input type="text" name="username" maxlength="30" value="<?php
echo htmlentities($username); ?>" />
</td>
</tr>
<tr>
<td>Password:</td>
<td>
<input type="password" name="password" maxlength="30" value="<?php
echo htmlentities($password); ?>" />
</td>
</tr>
<tr>
<td colspan="2">
<input type="submit" name="submit" value="login" />
</td>
</tr>
</table>
</form>
<?php include_layout_template('admin_footer.php'); ?>
答案 0 :(得分:5)
你有一个无限循环的重定向。
如果你还没有登录,您应该只在当前页面不是“login.php”时才进行重定向;即从该页面删除该逻辑。
答案 1 :(得分:2)
<? if(!$session->is_logged_in()){
redirect_to("login.php"); } ?>
我想你的问题就在于此。您正在检查登录页面,以查看是否有人登录。如果他们不是,您将重定向到您的登录页面,开始新的请求,它将再次执行检查。
人们无需登录即可使用登录页面,因此请在检查是否有人在使用该页面之前登录。
答案 2 :(得分:1)
<?php
和一个<?
(这也是一个坏主意。我坚持只使用{{1} })。