【问题标题】:Session code not working in php会话代码在 php 中不起作用
【发布时间】:2018-08-02 07:49:47
【问题描述】:

我正在尝试开发一个登录表单,所以我上网并选择了一个代码。我创建了一个名为“company”的数据库,其中包含一个包含字段的表“login”; ID、用户名和密码。我已经在表格中保存了一些条目。

当我尝试登录时,我得到了响应; 页面没有正确重定向; Firefox 检测到服务器正在以永远无法完成的方式重定向对该地址的请求。此问题有时可能是由禁用或拒绝接受 cookie 引起的。

我不知道出了什么问题。 下面是我的代码: index.php

<?php
include('login.php'); // Includes Login Script

if(isset($_SESSION['login_user'])){
header("location: profile.php");
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Login Form in PHP with Session</title>
<link href="style.css" rel="stylesheet" type="text/css">
</head>
<body>
<div id="main">
<h1>PHP Login Session Example</h1>
<div id="login">
<h2>Login Form</h2>
<form action="" method="post">
<label>UserName :</label>
<input id="name" name="username" placeholder="username" type="text">
<label>Password :</label>
<input id="password" name="password" placeholder="**********" type="password">
<input name="submit" type="submit" value=" Login ">
<span><?php echo $error; ?></span>
</form>
</div>
</div>
</body>
</html>

登录.php

<?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", "", "company");
// To protect MySQL injection for Security purpose

// SQL query to fetch information of registerd users and finds user match.
$query = mysqli_query($connection, "select * from login where password='$password' AND username='$username'");
$rows = mysqli_num_rows($query);
if ($rows == 1) {
$_SESSION['login_user']=$username; // Initializing Session

} else {
$error = "Username or Password is incorrect";
}
mysqli_close($connection); // Closing Connection
}
}
?>

注销.php

<?php
session_start();
if(session_destroy()) // Destroying All Sessions
{
header("Location: index.php"); // Redirecting To Home Page
}
?>

profile.php

<?php
include('session.php');
?>
<!DOCTYPE html>
<html>
<head>
<title>Your Home Page</title>
<link href="style.css" rel="stylesheet" type="text/css">
</head>
<body>
<div id="profile">
<b id="welcome">Welcome : <i><?php echo $login_session; ?></i></b>
<b id="logout"><a href="logout.php">Log Out</a></b>
</div>
</body>
</html>

session.php

<?php
// Establishing Connection with Server by passing server_name, user_id and password as a parameter
$connection = mysqli_connect("localhost", "root", "","company");
session_start();// Starting Session
// Storing Session
$user_check=$_SESSION['login_user'];
// SQL Query To Fetch Complete Information Of User
$ses_sql=mysqli_query("select username from login where username='$user_check'", $connection);
$row = mysqli_fetch_assoc($ses_sql);
$login_session =$row['username'];
if(!isset($login_session)){
mysqli_close($connection); // Closing Connection
header('Location: index.php'); // Redirecting To Home Page
}
?>

谢谢。

【问题讨论】:

  • 您是否检查过您是否为标题位置提供了正确的路径
  • 是的,所有文件都在同一个文件夹/var/www/html
  • 注意:mysqli 的面向对象接口明显不那么冗长,使代码更易于阅读和审核,并且不容易与过时的mysql_query 接口混淆。在您对程序风格投入过多之前,值得转换一下。示例:$db = new mysqli(…)$db-&gt;prepare("…") 过程化接口是 PHP 4 时代引入 mysqli API 时的产物,不应在新代码中使用。
  • 这段代码的一切都非常令人担忧,你应该不惜一切代价避免使用它。
  • 正如@tadman 指出的那样,如果您使用了OO 形式的界面,那么出现这个错误会更加困难。

标签: php mysql linux session


【解决方案1】:

问题解决了。

我已连接到另一个具有更多字段的表; id、名字、用户名和密码。

我编辑了 session.php 并使用 Firstname 更改了 mysqli_query,其中用户名为 $user_check。我现在打印登录用户的名称。

谢谢你们。

【讨论】:

  • 如果此代码用于学术目的,那么希望您已经学到了一些东西。如果这是用于实际用户的生产使用,那么还有许多尚未解决的严重问题,所以我会非常谨慎地进行。
  • 我正在学习,但打算构建一些应用程序。实际上,当提到我的代码中有任何严重问题时,我会非常高兴。这样我就能学到更多。谢谢@tadman
  • 注意安全,互联网上的世界很糟糕。
猜你喜欢
  • 2017-08-11
  • 2016-05-08
  • 1970-01-01
  • 2014-06-28
  • 2017-02-01
  • 2012-05-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多