Php脚本不能仅在Safari中工作(仅限在线 - 它可在本地进行测试)

时间:2015-06-25 22:18:57

标签: php debugging safari

此脚本适用于所有浏览器,但不适用于Apple Mac或Windows Safari。它适用于Chrome中的相同计算机。它适用于其他浏览器。 iOS没问题。它也适用于Safari的本地主机 - 只是不上线! - Mac Safari:登录发生,用户在index.php上短暂停留(毫秒)但立即被发送回登录页面...

下面的login_do.php ......

<?php
if ($_SERVER['REQUEST_METHOD']=='POST')
    {
        require ('includes/connect_db.php');
        require ('login_tools.php');
        list ($check,$data)=validate($con,$_POST['username'],$_POST['pass']);
        if ($check)
            {session_start();
                $_SESSION['user_id']=$data['user_id'];
                $_SESSION['username']=$data['username'];
                $_SESSION['admin']=$data['admin'];
                load ('index.php');
            }
        else {$errors=$data;}
        mysqli_close($con);
    }
include ('login.php');
?>

为什么?

这是POST到上述脚本的login.php。

<?php
$page_title='Login';
include ('includes/header.html');?>
<link rel="stylesheet" type="text/css" href="handover.css">
<?php 
if (isset($errors) && !empty($errors))
{
echo'<p id="err_msg">Oops! There was a problem:<br>';
foreach ($errors as $msg)
{
echo "- $msg<br>";
}
echo 'Please try again or <a href="help.php">Ask for help</a></p>';
}
?>
<div id="middler">
<h2>
<form action="login_do.php" method="POST">
<p>
User name: <input type="text" name="username"></p>
<p>
Password: <input type="password" name="pass"></p>
<p>
<input type="submit" name="Login"value="Login"></p>
</form>

</div>
<?php 
include ('includes/logoutfooter.html')?>

登录暂时登陆的index.php ......

<?php 
session_start();
if (isset($_SESSION['username']))
{
    $screenname=$_SESSION['username'];
}
if (isset($_SESSION['admin']))
{
    $admincheck=$_SESSION['admin'];
}

else {$url = 'login.php';
    echo '<META HTTP-EQUIV=Refresh CONTENT="0; URL='.$url.'">';}
?>

最后:login_tools.php

<?php
function load ($page = 'login.php')
{
$url = 'http://'.$_SERVER['HTTP_HOST'].dirname($_SERVER['PHP_SELF']);
$url=rtrim($url,'/\\');
$url.='/'.$page;
header("Location: $url");
exit();
}
function validate($con,$email='' , $pwd = '')
{
$errors=array();
if (empty($email))
{$errors[]='Enter your user name.';}
else
{$e=mysqli_real_escape_string($con,trim($email));}
if (empty($pwd))
{$errors[]='Enter your password.';}
else
{$p = mysqli_real_escape_string($con,trim($pwd));}

if (empty($errors))
{
$q="SELECT user_id, username, admin
FROM DPLA.users
WHERE username='$e'
AND password='$p'";
$r=mysqli_query($con, $q);
if (@mysqli_num_rows($r)==1)
{$row=mysqli_fetch_array($r,MYSQLI_ASSOC);
return array(true,$row);
}
else
{$errors[]='username and password not found.';}
}
return array(false, $errors);
}

1 个答案:

答案 0 :(得分:0)

发出重定向到else的元刷新的login.php子句仅附加到$_SESSION['admin']检查。因此,即使设置了$_SESSION['username'],也会重定向任何非管理员。将其更改为:

<?php 
session_start();
if (isset($_SESSION['username']))
{
    $screenname=$_SESSION['username'];
    if (isset($_SESSION['admin']))
    {
        $admincheck=$_SESSION['admin'];
    }
} else {
    $url = 'login.php';
    echo '<META HTTP-EQUIV=Refresh CONTENT="0; URL='.$url.'">';
}
?>