简体   繁体   English

无法进入首页,总是回到登录页面

[英]Can't enter the home page, always directing back to login page

As this page is owned by it users, so it has each credentials to enter it which it is by using login form of php (that's what I know so far, I am not very good in php, to be honest). 由于此页面是用户拥有的页面,因此它具有使用php登录表单输入的每个凭据(这是我到目前为止所知道的,老实说,我在php中不是很好)。

The problem I do really guess about this must be in the using of session function (and this is the most complicated things to me know, I am not very familiar of using this.) 我确实对此确实猜到的问题一定是在使用会话函数时(这是我所知道的最复杂的事情,我对使用它并不十分熟悉。)

In the config of the form, I set the session like this (Well, I just copy paste the code from somewhhere) as follow: 在表单的配置中,我将会话设置如下(好吧,我只是从某处复制粘贴代码),如下所示:

// User Redirect Conditions will go here
if($count==1)

{
    // Save type and other information in Session for future use.
    $_SESSION[type]=$row[0];
    $_SESSION[Region]=$row[1];
    $_SESSION[myemail]=$myemail;

    // if user type is ACTAdmin only then he can access protected page.
    if($row[0] == 'ACTAdmin') { 
        header( "location:index.php");  
    }
    else { 
        header( "location:login.html");  
    }

}

else 
{
header("location:login.html");
}
// Closing MySQL database connection 
$dbh = null;


In the head of the home page (and in each all related pages), I write a session start there like this: 在主页(以及所有相关页面)的开头,我像这样编写一个会话开始:

<?php 
include('UserSessionAdmin.php');
?>


In which it will get the data from UserSessionAdmin.php: 它将从UserSessionAdmin.php获取数据:

<?php
session_start();
if($_SESSION[type]!='ACTAdmin'){
header('location:login.html');
exit();
}
include('configPDO.php');
?>


What is included in the configPDO.php is here: configPDO.php中包含的内容在这里:

<?php
// mysql hostname
$hostname = 'mysql.com';
// mysql username
$username = 'alkushh';
// mysql password
$password = 'alkush';
// Database Connection using PDO
try {
$dbh = new PDO("mysql:host=$hostname;dbname=user", $username, $password);
}
catch(PDOException $e)
{
echo $e->getMessage();
}
?>


It's been more than two days for me just to solve it but I don't have any idea how to. 我已经解决了两天多了,但是我不知道如何解决。 Some people who are experts in here may help me with this thing, please. 请一些这里的专家来帮助我解决这个问题。

Thank you and regards, 谢谢你,谢谢



Here is the full script that define the $count==1 这是定义$ count == 1的完整脚本

<?php
// Start Session because we will save some values to session varaible.
session_start();

// include connection file
include("configPDO.php");

// Define $myusername and $mypassword
$myemail=$_POST['myemail']; 
$mypassword=$_POST['mypassword']; 

// We Will prepare SQL Query
$STM = $dbh->prepare("SELECT Type,Region FROM user WHERE myemail = :myemail AND mypassword = :mypassword");

// bind paramenters, Named paramenters alaways start with colon(:)
   $STM->bindParam(':myemail', $myemail);
   $STM->bindParam(':mypassword', $mypassword);

// For Executing prepared statement we will use below function
   $STM->execute();

// Count no. of records 
   $count = $STM->rowCount();

//just fetch. only gets one row. So no foreach loop needed :)
  $row  = $STM -> fetch();

// User Redirect Conditions will go here
   if($count==1)
   .....
   .....

在此处输入图片说明

Here it is 这里是

if ( $count == 1 ) {
    $_SESSION['login_id'] = $row['id']; // i prefer to name it login_id, you can use $row['id'] or $row[0]. but i prefer to write with the column name

    if ( $_SESSION['login_id'] == 1 ) { // it means if login id = 1 then go to index.php
        header("location: index.php");  
    } else { 
        header("location: login.html");  
    }
}

else { header("location: login.html"); }

i cut session region because you didnt have a region column and also i cut session myemail because you didnt need it 我剪切了会话区域,因为您没有区域列,并且我剪切了会话myemail,因为您不需要它


UserSessionAdmin.php UserSessionAdmin.php

<?php
    session_start();
    if ( $_SESSION['login_id'] == 0 || $_SESSION['login_id'] == '' ) {
        header('location: login.html');
        exit();
    }

    require_once('configPDO.php');
?>

Please turn on your error reporting to see, that there is no constants such as type , Region , myemail . 请打开错误报告,以查看没有常量,例如typeRegionmyemail Use " or ' around parameter of session: 在会话的参数周围使用"'

if (strcmp($_SESSION['type'], 'ACTAdmin') !== 0) {
    header('location:login.html');
    exit();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM