Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/xamarin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 单页登录持续出现500个服务器错误_Php_Session_Internal Server Error_Server Error - Fatal编程技术网

Php 单页登录持续出现500个服务器错误

Php 单页登录持续出现500个服务器错误,php,session,internal-server-error,server-error,Php,Session,Internal Server Error,Server Error,我想做的事 我正在创建一个带有登录表单的单页成员区 登录时,用户应该看到的唯一东西(在此阶段)是一个注销按钮 出了什么问题 只要数据库中有匹配项(例如用户名正确,或者用户名和密码都正确),就会出现500服务器错误 刷新页面,无论密码是否匹配,用户都会登录 当用户单击注销链接时,还会出现500服务器错误 代码 <?php session_start(); if(isset($_GET['logout'])&&$_GET['logout']==='true'){ l

我想做的事

我正在创建一个带有登录表单的单页成员区

登录时,用户应该看到的唯一东西(在此阶段)是一个注销按钮

出了什么问题

只要数据库中有匹配项(例如用户名正确,或者用户名和密码都正确),就会出现
500服务器错误

刷新页面,无论密码是否匹配,用户都会登录

当用户单击注销链接时,还会出现
500服务器错误

代码

<?php 
session_start();

if(isset($_GET['logout'])&&$_GET['logout']==='true'){
    logout();
    header('location: ./');
}

if(isset($_POST['submit'])) {
    $email = $_POST['email'];
    $password = $_POST['password'];

    $hostname = 'REDACTED';
    $username = 'REDACTED';
    $password = 'REDACTED';
    $dbname   = 'REDACTED';

    try {
        $dbh = new PDO("mysql:host=$hostname;dbname=$dbname", $username, $password);
    }

    catch(PDOException $e){
        echo($e->getMessage());
    }


    $stmt=$dbh->prepare('SELECT `password`, `id` FROM `tap_users` WHERE `email`=?');
    $stmt->execute(array($email));

    if($stmt->rowCount()==0){
        //incorrect username
        header('location: ./?error=user');
        exit();
    }
    $userData = $stmt->fetch(PDO::FETCH_ASSOC);

    if($password != $userData['password']) {
        //incorrect password
        header('location: ./?error=pass');
        exit();
    } else {
        validateUser($userData['id']);
        header('location: ./');
    }
}

?>

<head></head>

<body>

<?php
if(!isLoggedIn()) {
    show_login();
    exit();
}
?>

<a href="?logout=true">Logout</a>

</body>

</html>

<?php
//Show login form
function show_login(){
    echo    '<form method="post">
                <input type="text" name="email" placeholder="Email Address" />
                <input type="password" name="password" placeholder="Password" />
                <input type="submit" value="Submit" name="submit" />
            </form>

            </body>

            </html>';
}

//Check if a user is logged in
function isLoggedIn(){
    if(isset($_SESSION['valid']) && $_SESSION['valid'])
        return true;
    return false;
}

//validate the user
function validateUser($userid){
    //this is a security measure
    session_regenerate_id();
    $_SESSION['valid'] = 1;
    $_SESSION['userid'] = $userid;
}

//logout the user
function logout(){
    //destroy all of the session variables
    $_SESSION = array();
    session_destroy();
}
?>

您要做的是,如果用户登录正确,则您将重定向到标题('location:./');可能您没有index.php,或者apache配置中的目录索引处于关闭状态

做一件事把它改成

标题('location:index.php')

它会起作用的


这与php或mysql无关,这是服务器配置错误。

我已经运行了您的代码:语法似乎没有任何问题。问题可能来自文件的权限错误。文件是否具有用户的执行权限。在ftp中,确保权限设置为755。这应该允许每个人阅读和执行php代码。如果这不是问题所在,那么请联系您的主机并询问错误,他们应该能够向您提供该信息。另一个选项是在PHP中创建自己的错误日志。您可以查看以继续将所有错误显示在单独的文件中。

您查看过PHP错误日志吗?我还将从上面的代码片段中删除数据库用户名和密码。您知道如何使用浏览器开发工具上的
网络
选项卡吗?如果您单击其中一个请求,应该会有一个
响应
选项卡,它会告诉您错误。在Firefox或Chrome中:
F12->Network->500请求->响应
(注意,在
网络
选项卡中显示任何内容之前,可能需要重新加载页面)@ajtrichards我的主机提供商不提供对PHP错误日志的访问。当我打开ono PHP错误报告时没有错误。@Alex这是一行
if else
语句,在这种情况下他们不需要
{}
大括号。@TimLewis在阅读了您的编辑后,我刷新了页面,找到了
Response
选项卡-它说重定向有错误;将
header
函数从
location:./xxxx
更改为
location:index.php?xxxxx
成功了,谢谢-尽管现在无论密码是否正确,它都能正确登录。一次解决一个问题!这是正确的路线。。。从
location:./?xxx
更改为
location:index.php?xxx
,现在可以工作了。