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 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器