简体   繁体   English

是否使用php或javascript方法重定向页面

[英]whether to use php or javascript method for redirecting a page

Whenever I user php header("Location: index.php"), most of time then it does not work. 每当我使用php标头(“ Location:index.php”)时,大多数时候它就无法工作。 It exactly DOES NOT work without any error nor any run-time fatal error. 没有任何错误或任何运行时致命错误,它完全不起作用。 I don't use any echoing nor I keep empty lines prior to header() call. 我不使用任何回显,也不在header()调用之前保留空行。 I double check everthing, but it does not work. 我仔细检查了所有内容,但没有用。

Now, I user JS solution. 现在,我使用JS解决方案。 I write a function that gets the param, store it in an immediately created <input> field and the using Javascript in the same function, I get the value out of input and assign it to window.location.href= value; 我编写了一个获取参数的函数,将其存储在立即创建的<input>字段中,并在同一函数中使用Javascript,从输入中获取值并将其分配给window.location.href= value; which then finalizes the redirection. 然后完成重定向。 Is my method reliable and good? 我的方法可靠又好吗? My function seems like this: 我的功能似乎是这样的:

function redirect($address)
{
?>
<input id='this_address' value="<?php echo $address; ?>" />
window.location.href = document.getElementById("this_address").value;
<?php

} }

Are you sure that your index.php is in the same directory as the scripts that calls redirect or vice versa...? 您确定index.php与调用redirect的脚本位于同一目录中,反之亦然...?

If not try header("location:../../index.php"); 如果没有,请尝试header("location:../../index.php");

Setting and cookies or session variables will cause issues with a redirect. 设置和cookie或会话变量将导致重定向问题。 It has to be sent before any output or exchange with the server and the client. 必须在与服务器和客户端进行任何输出或交换之前发送它。 Check your browser log for any errors too. 还要检查浏览器日志中是否有任何错误。 It might be helpful in determining why you are not achieving the result you want. 这可能有助于确定为什么无法获得所需的结果。

You could also implement both methods, but it would be more useful to find out why your PHP redirect is not effective. 您也可以实现这两种方法,但是找出为什么PHP重定向无效的方法会更有用。

If u can't able to find out the solution for php header prob, Just add the below code at the top of the page. 如果您无法找到php header prob的解决方案,只需在页面顶部添加以下代码。 This is not the perfect way, but we can use this... 这不是完美的方法,但是我们可以使用此方法...

ob_start();

为什么不使用Response.Redirect "../../index.php"来重定向到另一个页面。

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

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