duan36000 2019-07-13 13:16
浏览 104
已采纳

每次在服务器上使用php mysql重定向到登录页面的登录脚本

i have a login for like the following:

    <form action="authenticate.php" method="post">
                <label for="username">
                    <i class="fas fa-user"></i>
                </label>
                <input type="text" name="username" placeholder="Username" id="username" required>
                <label for="password">
                    <i class="fas fa-lock"></i>
                </label>
                <input type="password" name="password" placeholder="Password" id="password" required>
                <input type="submit" value="Login">
            </form>

below is my authenticate.php

<?php
session_start();
include('config.php');
if ( mysqli_connect_errno() ) {

    die ('Failed to connect to MySQL: ' . mysqli_connect_error());
}
if ( !isset($_POST['username'], $_POST['password']) ) {
    
    die ('Please fill both the username and password field!');
}


if ($stmt = $link->prepare('SELECT id, password FROM accounts WHERE username = ?')) {

    $stmt->bind_param('s', $_POST['username']);
    $stmt->execute();

    $stmt->store_result();
  if ($stmt->num_rows > 0) {
    $stmt->bind_result($id, $password);
    $stmt->fetch();
    
    if (password_verify($_POST['password'], $password)) {
        
        $_SESSION['loggedin'] = TRUE;
        $_SESSION['name'] = $_POST['username'];
        $_SESSION['id'] = $id;
        header('Location: index.php');
        exit();
    } else {
        echo 'Incorrect password!';
    }
} else {
    echo 'Incorrect username!';
}
$stmt->close();
}

i have used the following php code on every header so that the user is redirected if not logged in

<?php
// We need to use sessions, so you should always start sessions using the below code.
session_start();

include('config.php');
// If the user is not logged in redirect to the login page...
if (!isset($_SESSION['loggedin'])) {
    header('Location: login.php');
    exit();
}
?>

everything is working fine on my localhost, now i uploaded it to server, live the problem is even when i login i am again redirected to login page. here is my website for reference: enter link description here can anyone tell me what could be the problem with my code?

</div>
  • 写回答

2条回答 默认 最新

  • drkj41932 2019-07-15 08:48
    关注

    i have found out the problem, in future if anyone has the same error hope it helps

    domain was pointing to .html website, so the php.ini file was not there, now its added and working fine

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog