【发布时间】:2018-01-08 11:59:08
【问题描述】:
我创建了一个登录系统,登录后用户将被重定向到profile.php。在profile.php 页面中,标题将显示Welcome [username]
Session.php:
$connection = mysqli_connect("localhost", "root", "");
$db = mysqli_select_db($connection, "id2290767_wp");
session_start();
$user_check = isset($_SESSION['login_user']);
$ses_sql = mysqli_query($connection, "select name from login where name='$user_check'");
$row = mysqli_fetch_assoc($ses_sql);
$login_session = $row['name'];
if(!empty($login_session)) {
mysqli_close($connection);
header('Location: members.php');
}
profile.php(带有问候的那个)
<?php
include('session.php');
if(!isset($_SESSION['login_user'])){
header("location: profile.php");
}
?>
<h1>Welcome <i><?php echo $login_session; ?></h1>
members.php中与登录表单相关联的signin.php:
session_start(); // Starting Session
$error = ''; // Variable To Store Error Message
if(isset($_POST['submit'])) {
if(empty($_POST['username']) || empty($_POST['password'])) {
$error = "Username or Password is invalid";
} else {
// Define $username and $password
$username = $_POST['username'];
$password = $_POST['password'];
// Establishing Connection with Server by passing server_name, user_id and password as a parameter
$connection = mysqli_connect("localhost", "root", "");
// To protect MySQL injection for Security purpose
$username = stripslashes($username);
$password = stripslashes($password);
$username = mysqli_real_escape_string($connection, $username);
$password = mysqli_real_escape_string($connection, $password);
// Selecting Database
$db = mysqli_select_db($connection, "id2290767_wp");
// SQL query to fetch information of registerd users and finds user match.
$query = mysqli_query($connection, "select * from login where password='$password' AND name='$username'");
$rows = mysqli_num_rows($query);
if($rows == 1) {
$_SESSION['login_user'] = $username; // Initializing Session
header("location: profile.php"); // Redirecting To Other Page
} else {
$error = "Username or Password is invalid";
}
mysqli_close($connection); // Closing Connection
}
}
members.php:
<?php
include('signin.php'); // Includes Login Script
if(isset($_SESSION['login_user'])){
header("location: profile.php");
}
但是我在 PHP 页面中没有得到任何文本。它只是说“欢迎”。
我做错了什么?
编辑:更改后获得无限循环
$user_check = isset($_SESSION['login_user']);
到
$user_check=isset($_SESSION['login_user']) ? $_SESSION['login_user'] : "";
【问题讨论】:
-
您的欢迎页面是
members.php?我说的对吗? -
不,欢迎页面位于 profile.php,memebrs.php 有登录表单,如果有会话,则重定向到 profile.php
-
那么您在
members.php页面上有一些$_POST或$_GET值?不是吗(如果您是通过登录表单来的)? -
添加了整个代码
标签: php mysql sql session login