【问题标题】:header location redirect not working标头位置重定向不起作用
【发布时间】:2014-05-26 18:51:09
【问题描述】:

我有一个登录页面,成功登录后,我会重定向到用户来自的页面或 index.php。在我实现 ajax 登录之前,这很好用。问题是成功登录后页面重定向到同一页面而不是索引。它只是重定向到那里并在 login.php 中显示 index.php 页面

网址看起来像localhost/login.php?/index.php

js

$('.login').on('click', function() { 
var user = $('.username').val();
var pass = $('.password').val();
ref;//redirect url
$.ajax({
type: "GET",
url: "process.php",
data: {req: 'req', user: user, pass:pass, ref:ref},
cache: false,
success: function(response)
{  
}
});

});

process.php 我从 js 获取每个值,但重定向不起作用。有什么想法吗?

if(isset(_GET['req'])){ 
if(empty($_GET['user'])){
echo "uname empty";
}
else if(empty($_GET['pass'])){
echo "pass empty";
}
else{
if(...){
//some validations
}
else{
//everything good...redirect to index.php or user's page
 $page = $_GET['ref'];
if(!empty($page)){     
//Redirect to the page where user came from
header("location: ".$page);
}
else{header("location:index.php");}
}
}
}

【问题讨论】:

  • 重定向 AJAX 请求有点毫无意义。要么使用表单正常提交请求,要么在成功回调中使用 JavaScript 在 AJAX 请求成功后重定向(在这种情况下,您可能希望返回某种状态代码)。并且为了一切对您来说是神圣的:通过 GET 提交登录表单是不可接受的。这会导致密码出现在服务器日志中。总是使用 POST!
  • @FerozAkbar 做到了,但没有任何改变。
  • @ThiefMaster Okay 将其更改为 post。

标签: php redirect


【解决方案1】:

这样做

$page = $_GET['ref'];
if(!empty($page)){     
//Redirect to the page where user came from
echo $page;
}
else{echo 'index.php';}
}

在 ajax 响应中

success: function(response)
{  
  location.href='/'+response;
} 

【讨论】:

    猜你喜欢
    • 2022-01-17
    • 1970-01-01
    • 1970-01-01
    • 2014-11-19
    • 1970-01-01
    • 2012-11-20
    • 2023-03-19
    • 2016-12-20
    相关资源
    最近更新 更多