【问题标题】:Issue getting session variable to work问题让会话变量工作
【发布时间】:2016-04-23 00:44:52
【问题描述】:

我正在尝试让我的login.php 重定向到我在main.php 中的主页,并在成功时回显登录消息,或者在登录失败时回显不成功消息。它忽略了脚本的快速部分并指导我:

地点:http://localhost/projects/ibill_v3/html/loginformfail.html#home

这甚至不存在。有没有办法解决这个问题还是我让它太复杂了?任何帮助将不胜感激!

main.php(主页)

<?php
    session_start();
include "loginform.php";
if (isset($_SESSION['user_session']) and $_SESSION['user_session']!=""){
  echo 'working';
}
else {
  echo 'not working';
}
?>

loginform.php

<?php 
$con=mysqli_connect('localhost','root','cornwall','ibill');
// This code creates a connection to the MySQL database in PHPMyAdmin named 'ibill':

$username="";
$password="";

if (isset ($_POST['username'])){
$username = mysqli_real_escape_string($con, $_POST['username']);
}
if (isset ($_POST['password'])){
$password = mysqli_real_escape_string($con, $_POST['password']);
}
//These are the different PHP variables that store my posted data.

$login="SELECT * FROM users WHERE username='$username' AND password='$password'";
$result=mysqli_query($con, $login);
$count=mysqli_num_rows($result);
//This is the query that will be sent to the MySQL server.
if($count==1)
{
  $_SESSION["user_session"]=$username;
  header('Location: http://localhost/projects/ibill_v3/html/main.php#home');
  exit();
}
//This checks the 'user_details' database for correct user registration details and if successful, directs to home page.
else {
   header('Location: http://localhost/projects/ibill_v3/html/loginformfail.html');
   exit();
}
//If login details are incorrect

/** Error reporting */
error_reporting(E_ALL);
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
?>

【问题讨论】:

  • 我看不到您在 loginform 上开始会话的位置
  • 您的查询实际上是否返回了结果? $count真的等于1吗?你应该散列你的密码。以纯文本形式存储它们是一个非常非常糟糕的主意。
  • @andre3wap 我需要在“loginform.php”上启动会话吗?谢谢马库斯,是的,注释掉时计数等于 1,并且在我试图让会话正常工作之前,该部分代码正在工作。将在完成前对密码进行哈希处理
  • @asharoo85 "我是否需要在 'loginform.php' 上启动会话。" 不需要。这不是必需的,会发出通知。您只需要启动一次会话,因为它将在include'd 文件的范围内。
  • 请使用 PHP 的built-in functions 来处理密码安全问题。如果您使用低于 5.5 的 PHP 版本,您可以使用 password_hash() compatibility pack。在散列之前,请确保您 don't escape passwords 或对它们使用任何其他清理机制。这样做会更改密码并导致不必要的额外编码。

标签: php html session jquery-mobile login


【解决方案1】:

第1步:在login.php中设置提交到loginform.php的动作(action="loginform.php")

step2:在 loginform.php 中启动会话并将重定向位置更改为 main.php

<?php 
session_start();
$con=mysqli_connect('localhost','root','cornwall','ibill');
// This code creates a connection to the MySQL database in PHPMyAdmin named 'ibill':

$username="";
$password="";

if (isset ($_POST['username'])){
$username = mysqli_real_escape_string($con, $_POST['username']);
}
if (isset ($_POST['password'])){
$password = mysqli_real_escape_string($con, $_POST['password']);
}
//These are the different PHP variables that store my posted data.

$login="SELECT * FROM users WHERE username='$username' AND password='$password'";
$result=mysqli_query($con, $login);
$count=mysqli_num_rows($result);
//This is the query that will be sent to the MySQL server.
if($count==1)
{
  $_SESSION["user_session"]=$username;
  header('Location:main.php');
  exit();
}
//This checks the 'user_details' database for correct user registration details and if successful, directs to home page.
else {
   header('Location: main.php');
   exit();
}
//If login details are incorrect

/** Error reporting */
error_reporting(E_ALL);
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
?>

第 3 步:在 main.php 中删除包含“loginform.php”;

<?php
    session_start();
if (isset($_SESSION['user_session']) and $_SESSION['user_session']!=""){
  echo 'working';
}
else {
  echo 'not working';
}
?>

【讨论】:

  • 刚刚试了一下,效果很好。谢谢@Bitto Bennichan
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-04-10
  • 2013-07-14
  • 1970-01-01
  • 1970-01-01
  • 2021-12-30
  • 2017-09-20
相关资源
最近更新 更多