dsh1956 2016-08-26 15:10
浏览 39

PHP页面登录有两个文件夹

I have page login.php :

<?php 

include '../f-connect.php';

if(isset($_POST) && !empty($_POST['login']) && !empty($_POST['password'])){
     extract($_POST);
     $password = sha1($password);
     $sql = "SELECT u.*, r.* FROM users as u INNER JOIN roles AS r ON r.id_roles = u.roles_id WHERE u.login = '$login' AND u.password = '$password' AND r.name  = 'provider'";
     $req = $db->prepare($sql);
     $req->execute();
    if($req->rowCount() > 0){
        $data = $req->fetch();
        $_SESSION['Auth']= array(
            'id_user' => $data['id_user'],
            'firstname' => $data['firstname'],
            'lastname' => $data['lastname'],
            'login' => $data['login'],
            'password' => $data['password'],
            'roles_id' => $data['roles_id'],
            'id_roles' => $data['id_roles'],
            'name' => $data['name'],

        );




            $_SESSION['flash']['success'] = "You are now connected";
        header("Location:../Provider/profilProvider.php");
    }else{
        echo '<div class ="alert alert-warning">Login or password incorrect</div>';
    }

}elseif(isset($_POST) && !empty($_POST['login']) && !empty($_POST['password'])){
     extract($_POST);
     $password = sha1($password);
     $sql = "SELECT u.*, r.* FROM users as u INNER JOIN roles AS r ON r.id_roles = u.roles_id WHERE u.login = '$login' AND u.password = '$password' AND r.name  = 'customer'";
     $req = $db->prepare($sql);
     $req->execute();
    if($req->rowCount() > 0){
        $data = $req->fetch();
        $_SESSION['Auth']= array(
            'id_user' => $data['id_user'],
            'firstname' => $data['firstname'],
            'lastname' => $data['lastname'],
            'login' => $data['login'],
            'password' => $data['password'],
            'roles_id' => $data['roles_id'],
            'id_roles' => $data['id_roles'],
            'name' => $data['name'],

        );


            $_SESSION['flash']['success'] = "You are now connected";
        header("Location:../customer/profilCustomer.php");
    }else{
        echo '<div class ="alert alert-warning">Login or password incorrect</div>';
    }

}
?>

And i have two folders that contain:

folder one : 
provider : 
   ->profilProvider.php
   ->listProviders.php
   ->addProvider.php
   ->updateProvider.php



folder two : 
   customer : 
    ->profilCustomer.php
    ->listCustomers.php
    ->addCustomer.php
    ->updateCustomer.php

And i have table users :

 - id_user : int 
 - firstname :varchar(100) 
 - lastname : varchar(100) 
 - login : varchar(100) 
 - password : varchar(100) 
 - roles_id : int

And table roles :

- id_roles : int
- name : varchar(100)  ['provider', 'costumer']

when I would enter the login and password I want to redirect to one of these two folders

for example :

  • if the role equal provider I would go to the profilProvider.php page in the provider folder
  • if the role equal costumer I would go to the profilCostumer.php page in the costumer folder

But not direct me in associated folders

  • 写回答

1条回答 默认 最新

  • dongyinzheng6572 2016-08-28 06:02
    关注

    You have to provide the exact path to the file as per the header location is considered based on the project structure.

    My Project - (Main Folder) 
    login.php 
    provider - (Sub Folder One)
    ->profilProvider.php    
    ->listProviders.php     
    ->addProvider.php       
    ->updateProvider.php 
    customer - (Sub Folder Two)     
    ->profilCustomer.php    
    ->listCustomers.php    
    ->addCustomer.php   
    ->updateCustomer.php
    

    The header location what you have given in the both the conditions will cause you an error since your path to the folder/directory seems incorrect.

    Reason:

    Your project structure looks like the above one. My doubt is only with the include '../f-connect.php'; since the path provided here seems to be incorrect with your project structure.

    If this seems correct then you have to replace the header location alone in your code as i suggest now.

    Note: Since "../" will go back one directory from the Current working folder. As per you jsfiddle explanation you have your login.php file inside the project folder. and hence you have to change the header location alone.

    Solution for needing ob_start() in your project.

    If you find header already sent Error while redirection you can use ob_start() after the include '../f-connect.php' line.

    Replaced Code:

    <?php
    // Database Connectivity
    include '../f-connect.php';
    // Checking condition based on the form Submission
    if(isset($_POST) && !empty($_POST['login']) && !empty($_POST['password'])){
         extract($_POST);
         $password = sha1($password);
         $sql = "SELECT u.*, r.* FROM users as u INNER JOIN roles AS r ON r.id_roles = u.roles_id WHERE u.login = '$login' AND u.password = '$password' AND r.name  = 'provider'";
         $req = $db->prepare($sql);
         $req->execute();
        if($req->rowCount() > 0){
            $data = $req->fetch();
            session_start();
            $_SESSION['Auth']= array(
                'id_user' => $data['id_user'],
                'firstname' => $data['firstname'],
                'lastname' => $data['lastname'],
                'login' => $data['login'],
                'password' => $data['password'],
                'roles_id' => $data['roles_id'],
                'id_roles' => $data['id_roles'],
                'name' => $data['name'],
    
            );
            $_SESSION['flash']['success'] = "You are now connected";
            // This header Location will provide you an error 
            // since "../" will go back one directory from the Current working folder.
            // As per you jsfiddel explanation you have your login.php file inside the project folder.
            //header("Location:../Provider/profilProvider.php");
            header("Location: provider/profilProvider.php");
            }else{
            echo '<div class ="alert alert-warning">Login or password incorrect</div>';
            }
    
        }
        else if(isset($_POST) && !empty($_POST['login']) && !empty($_POST['password'])){
         extract($_POST);
         $password = sha1($password);
         $sql = "SELECT u.*, r.* FROM users as u INNER JOIN roles AS r ON r.id_roles = u.roles_id WHERE u.login = '$login' AND u.password = '$password' AND r.name  = 'customer'";
         $req = $db->prepare($sql);
         $req->execute();
        if($req->rowCount() > 0){
            $data = $req->fetch();
            session_start();
            $_SESSION['Auth']= array(
                'id_user' => $data['id_user'],
                'firstname' => $data['firstname'],
                'lastname' => $data['lastname'],
                'login' => $data['login'],
                'password' => $data['password'],
                'roles_id' => $data['roles_id'],
                'id_roles' => $data['id_roles'],
                'name' => $data['name'],
            );
            $_SESSION['flash']['success'] = "You are now connected";
            header("Location: customer/profilCustomer.php");
        }else{
            echo '<div class ="alert alert-warning">Login or password incorrect</div>';
        }
    }
    ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程