必须登录两次才能访问PHP中的其他页面

时间:2016-04-15 05:06:34

标签: php

我正在为我的网站构建管理员登录页面。我确定我的脚本有什么问题,但我必须登录两次才能重定向到管理主页。任何人都能帮我解决我做错了什么或者我错过了什么? 另外,我在localhost中尝试过,脚本工作正常,只需要登录一次。我把它放在我的主机后,我必须登录两次。 谢谢。

<?php 
session_start(); 
include_once("../config.php");
if(isset($_SESSION['name'])) {
    header('location:viewallitems.php');
}?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>
     <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
     <title>Login</title>
     <link href="style.css" type="text/css" rel="stylesheet" />
</head>

<body>
<?php
if(isset($_POST['submit'])) {
$errors = array();
$required = array('email','pword');
foreach($required as $fieldname) {
    if(!isset($_POST[$fieldname]) || empty($_POST[$fieldname])) {
        $errors[]= "The <strong> {$fieldname} </strong> was left blank";
    }
}//End: foreach
if(empty($errors)) {
    $email = mysqli_real_escape_string($mysqli,$_POST['email']);
    $pword = mysqli_real_escape_string($mysqli,$_POST['pword']);
    $hash_pw = $pword;

    $query = "SELECT CONCAT_WS(' ', first_name, last_name) 
              AS name
              FROM admin
              WHERE email='$email'
              AND pword='$hash_pw'
              LIMIT 1";
    $result = mysqli_query($mysqli,$query) or die(mysqli_error());
    if(mysqli_num_rows($result) == 1) {
        while($rows = mysqli_fetch_array($result,MYSQLI_ASSOC)) {
            $_SESSION['name'] = $rows['name'];
            header('location: viewallitems.php');
        }
    } else {
        $errors[] = "The email or password do not match those on file.";
    }
}

}//End if($_POST['submit'])
else {
    if(isset($_GET['stat']) && $_GET['stat'] == 1) {
        $message = "<ul><li>You are now logged out.</li></ul>";
    }
}
?>
    <div id="wrapper">
    <div class="bg"><a href="../index.html" target="_blank"><img src="../images/logo.png" /></a></div>
    <h1>Login To Admin Panel</h1>
        <?php if(!empty($errors)) {
            echo "<ul>";
            foreach($errors as $error) {
                echo "<li>{$error}</li>";
            }
            echo "</ul>";
        }?>

        <?php if(isset($message)) echo $message;?>
        <form action="" method="post">
            <center><img src="../images/avatar.png"></center>
            <p>
                <label for="email">Email:</label>
                <input type="text" size="35" placeholder="Enter Email" name="email"/>
            </p>

            <p>
                <label for="password">Password:</label>
                <input type="password" size="35" placeholder="Enter Password" name="pword" />
            </p>

            <p>
                <input class="blue" type="submit" name="submit" value="LOG IN" />
            </p>
        </form>
        <div class='login'>
            <a href="#">Login</a>
        </div>
    </div>
</body>
</html>

2 个答案:

答案 0 :(得分:1)

  1. 使用ob_start();

  2. 在第四行前删除php。

  3. 将doctype更改为HTML5 doctype

  4. 在所有可能妨碍header()函数的php.i之后,重新定位HTML(从第7行开始)到新位置。无论如何,将所有人聚集在一个地方总是好的。

  5. 新增,补充道:

    1. 使用mysql_free_result($ result)
    2. 释放结果内存

答案 1 :(得分:0)

当您重定向到viewallitems.php时,请检查浏览器中的网址,是否使用www。登录时,您的域名面前,然后将其重定向到非www?因为php将www.yourdomain.com/viewallitems.php和yourdomain.com/viewallitems.php视为两个不同的域,因此会话不会被共享。如果您登录www.yourdomain.com/login.php,请尝试访问www.yourdomain.com/viewallitems.php,我认为您不需要再次登录。

如果您遇到问题,我可以考虑几个解决方案:

1.使用.htaccess文件重写模式,以便将您网站中的所有页面重定向到www。版本或您域名的非www版本。这样当您输入yourdomain.com/login.php时,它将自动重定向到www.yourdomain.com/login.php

的.htaccess

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^yourdomain.com$
RewriteRule (.*) http://www.yourdomain.com/$1 [R=301,L] 

2.在php中实际上有一个共享会话的功能,名为 session_name session_set_cookie_params 。请查看以下文档:

http://php.net/manual/en/function.session-name.php

http://php.net/manual/en/function.session-set-cookie-params.php

它曾经工作,然后它不起作用。我现在不太确定,他们是否解决了这个问题。

3.设置域名www.yourdomain.com版本和非www.yourdomain.com版本的会话。

我更喜欢使用第一个选项,因为它限制了浏览器将您的网站视为两个不同域的可能性。

相关问题