Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/247.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 登录后未重定向标头_Php_Html_Redirect_Mysqli_Login - Fatal编程技术网

Php 登录后未重定向标头

Php 登录后未重定向标头,php,html,redirect,mysqli,login,Php,Html,Redirect,Mysqli,Login,你好,伙计,我希望有人能帮我。我见过许多相关的问题,并试图实施它,但没有运气 下面是我的登录代码: <?php session_start(); if(isset($_SESSION['user_type']) && isset($_SESSION['user_id'])) { header('Location: profile.php'); } if(is_file('include/connec

你好,伙计,我希望有人能帮我。我见过许多相关的问题,并试图实施它,但没有运气

下面是我的登录代码:

            <?php
    session_start();
    if(isset($_SESSION['user_type']) && isset($_SESSION['user_id']))
    {
      header('Location: profile.php');
    }
    if(is_file('include/connection.php'))
    include_once('include/connection.php');
    else
    exit('Database FILES MISSING:(');
    ?>
    <?php $_SESSION['main_title']  = "Login"; ?>
    <?php
    if(isset($_POST['submit'])){

      foreach($_POST as $key => $value){

        if(empty($value)){
          $_SESSION['main_notice'] = $key." is required, fill all fields";  
          $empty = true;    
          break;
        }
      }

      if(isset($empty) && $empty){
        header('Location: '.$_SERVER['PHP_SELF']);
      }
      else
      {
          extract($_POST);
          //$loginQuery = "select * from user where user_name = '$name' AND password = '$password' AND user_type = '$user_type'";
          $loginQuery = "select * from user where user_name = '$name' AND password = '$password'";
          $result = mysqli_query($con,$loginQuery);
          if(mysqli_num_rows($result)){

              $row = mysqli_fetch_array($result);               
              $_SESSION['user_id'] = $row['id'];
              $_SESSION['user_name'] = strtoupper($row['name']);
              $user_type = strtolower($row['user_type']);
              if(strtolower($user_type) == 'member'){   

                $_SESSION['user_type'] = 'member';  
                //header('Location: member-dashboard-home.php');
                header('Location: profile.php');

              }elseif(strtolower($user_type) == 'admin' || strtolower($user_type) == 'leader'){

                $_SESSION['user_type'] = strtolower($user_type);                                        
                //header('Location: admin-dashboard-home.php');
                header('Location: profile.php');
              }


          }else{
              $_SESSION['main_notice'] = "Invalid Details";
              header('Location: '.$_SERVER['PHP_SELF']);
          }
      }
    }
    ?>
    <?php 
    if(is_file('include/header.php'))
    include_once('include/header.php');
    ?>
    <?php if(isset($_SESSION['main_notice'])) { ?>      
            <div class="main-notice">
              <p>
                      <?php
                      echo $_SESSION['main_notice']; 
                      unset($_SESSION['main_notice']);
                      ?>
              </p>
            </div>  
    <?php } ?>      
    <div>
      <form name="login" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
      <table>

        <tr>
          <td>User Name</td> 
          <td><input type="text" name="name" required><br></td>
        </tr>
        <tr>
          <td>Password:</td>
          <td><input type="password" name="password" required><br></td>
        </tr>
        <!-- <tr>
          <td>User Type</td>
          <td>
            <label for="user_admin">Admin</label><input type="radio" value = "admin" name="user_type" id="user_admin" required>
            <label for="user_member">Member</label><input type="radio" value = "member" name="user_type" id="user_member" required>
          </td>
        </tr> -->
        <tr>
          <td></td>
          <td><input type="submit" name="submit" value="Login"></td>
        </tr>

        <tr>
          <td></td>
          <td><a href="register.php">Register</a></td>
        </tr>
      </table>
      </form>   
    </div>
    <?php 
    if(is_file('include/footer.php'))
    include_once('include/footer.php');
    ?>



您可以使用javascript执行重定向

function redirect($url) {
    echo '<script>window.location = "'.$url.'"</script>';
    die;
}

编辑:如果不清楚的话,这是一个PHP函数。

一些合理的代码缩进将是一个好主意。它帮助我们阅读代码,更重要的是,它将帮助您为自己的利益调试代码。可能会要求您在几周/几个月内修改此代码,最后您会感谢我将解释问题的任何内容输出到浏览器。或者,查看php错误日志会给您一个提示,因为它使代码更具可读性,在这里发布时是一个错误。只有在之前没有输出的情况下,标题才能工作。你查过了吗?输出包括类似以下空格的空格
。你看到了吗?谢谢你,那么我如何把它分配到我的当前页面呢。我是放在顶部还是底部。很抱歉,我对php一无所知。我不知道你是如何组织你的项目的,但是你可以在session_start()之后插入函数;如果你没有一个单独的文件来存储一般函数,或者类似的东西,比如引导或一些类。要执行重定向,请替换标题(“Location:profile.php”);带有重定向(“profile.php”);嗨,海索林。我设法听从你的指示。目前我没有额外的功能商店。我必须设法让它工作。这是我放置代码的屏幕截图。现在删除
标题(“location:profile.php”)
和put
重定向(“profile.php”)相反。是的,他已经这样做了,现在工作。谢谢,谢谢。
redirect("profile.php");