dongsuiwo0279 2016-04-22 14:57
浏览 34
已采纳

发出使会话变量起作用的问题

I am trying to get my login.php to redirect to my home page in main.php and echo a logged in message when successful, or echo an unsuccessful message if login fails. It is ignoring the fast part of the script and directing me to:

Location: http://localhost/projects/ibill_v3/html/loginformfail.html#home

This doesn't even exist. Is there a way to fix this or am i making it too complicated? Any help would be greatly appreciated!

main.php (home page)

<?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);
?>
  • 写回答

1条回答 默认 最新

  • dousi4950 2016-04-22 16:44
    关注

    step 1: set the action on submit to loginform.php in login.php (action="loginform.php")

    step2:in loginform.php start a session and change redirection location to 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);
    ?>

    step 3: In main.php remove include "loginform.php";

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

    </div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 运筹学中在线排序的时间在线排序的在线LPT算法
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试,帮帮忙吧