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

时间:2019-07-13 13:16:47

标签: php html sql

我的登录名如下:

	<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>

下面是我的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();
}

我在每个标头上都使用了以下php代码,以便在未登录的情况下重定向用户

<?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();
}
?>

一切都在我的本地主机上正常运行,现在我将其上传到服务器上,即使我登录后,问题仍然存在,我再次重定向到登录页面。这是我的网站供参考:  enter link description here 谁能告诉我我的代码可能是什么问题?

2 个答案:

答案 0 :(得分:0)

我已经找到了问题所在,以后如果有人遇到同样的错误,希望对您有帮助

域指向.html网站,因此php.ini文件不存在,现在已添加并可以正常工作

答案 1 :(得分:0)

在分配会话之前使用session_start()

if (password_verify($_POST['password'], $password)) {
        session_start();  
        $_SESSION['loggedin'] = TRUE;
        $_SESSION['name'] = $_POST['username'];
        $_SESSION['id'] = $id;
        header('Location: index.php');
        exit();