简体   繁体   English

重定向功能无法正常工作-PHP

[英]Redirect function doesn't work well - PHP

Hi I have the following PHP code: 嗨,我有以下PHP代码:

 function redirect()
 {
     header("Location: index.php");
 }
 session_start();

 if(isset($_SESSION['userName'] ))
    redirect();


 if($_SERVER['REQUEST_METHOD'] == 'POST')
 {   //more code goes here...
   redirect();
  }

The problem is that the function redirect works only in the following condition: 问题在于功能redirect仅在以下情况下有效:

if($_SERVER['REQUEST_METHOD'] == 'POST')

Why and how can I fix it? 为什么以及如何解决?

Thanks! 谢谢!

Can you try this, 你可以试试这个吗

    session_start();
    function redirect()
    {
        header("Location: index.php");
    }

    if(isset($_SESSION['userName'])){
        redirect();
    }elseif($_SERVER['REQUEST_METHOD'] == 'POST'){   
     //more code goes here...
     redirect();
    }

What happens otherwise? 否则会怎样? Is there any error? 有什么错误吗? Something, that happens there? 发生什么事了?

We need more Information! 我们需要更多信息!

This should help: 这应该有助于:

<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
?>

it works in that condition becuase you are calling it in that condition... maybe if you use some tabs you can see why 它可以在这种情况下工作,因为您在那种情况下调用了它……也许,如果您使用一些标签,您会发现为什么

take a look 看一看

//your function
function redirect(){
    header("Location: index.php");
}
//here your function end

//this is not part of the function
session_start();
if(isset($_SESSION['userName'] ))
    redirect();

if($_SERVER['REQUEST_METHOD'] == 'POST') {   
    //more code goes here...
   redirect();
}

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

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