gpt4 book ai didi

java - PHP 中的服务器端重定向

转载 作者:行者123 更新时间:2023-11-29 09:46:31 26 4
gpt4 key购买 nike

我有一个 Java 应用程序,需要将我们现有的 PHP 网站与之集成。供应商希望我们执行服务器端重定向以允许安全身份验证和单点登录,但我不确定如何在 PHP 中执行此操作。供应商解释工作流程如下:

  1. 用户点击我们 PHP 网站上的“打开应用程序”链接
  2. PHP 应用程序点击 Java 应用程序上的页面,发送身份验证参数
  3. 如果成功,PHP 应用程序会将 header 发送回用户的浏览器,这会强制“重定向”,否则 PHP 应用程序会显示错误

这将使我们的 PHP 应用程序能够安全地与 Java 应用程序对话,而客户端永远不必发送任何类型的身份验证。

据我所知,.NET 和 Java 内置了这种功能,但我无法在 PHP 中找到执行此操作的方法。有任何想法吗?

更新

我不是在谈论使用 header("Location: ...");函数做一个重定向。这个服务器端重定向的关键是应用程序进行身份验证并将所有信息发送回客户端,以便客户端登录。使用 header("Location: ...") 只是强制浏览器去别处。

更新 2

autologin.php(模拟用户通过curl登录到外部应用)

// The login 'form' is at login.php
$ch = curl_init('http://domain.local/login.php');
// We are posting 2 variables, and returning the transfer just so it doesn't dump out
// Headers are processed by the callback function processHeaders()
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_HEADERFUNCTION, 'processHeaders');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, 'username=user&password=pass');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Execute curl, close the connection, and redirect the user to a 'restricted' page
$response = curl_exec($ch);
curl_close($ch);
header("Location: http://domain.local/restricted.php");

function processHeaders($ch, $header) {
// Dump the response headers to the client
header($header);
strlen($header);
}

login.php(包含“登录”表单)

session_start();
if($_POST) {
if($_POST['username'] == 'user' && $_POST['password'] == 'pass') {
$_SESSION['auth'] = 1;
$_SESSION['token'] = md5(time());
} else {
echo 'Auth failed';
}
} else {
echo 'Invalid access type';
}

restricted.php(受限页面)

session_start();
if($_SESSION['auth']) {
echo 'Secret Token: '.$_SESSION['token'];
} else {
echo 'Please log in';
}

这个想法是用户希望最终到达“restricted.php”。 “login.php”包含登录所需的代码。我想要模拟的是用户在“login.php”上填写表单并将用户登录到“restricted.php”。

上面的代码片段在我的本地测试中协同工作(点击 autologin.php 重定向到 restricted.php 并打印出 secret token ),但我似乎无法让它跨应用程序工作。这些应用程序将位于同一域( https://domain.com/myapphttps://domain.com:1234/vendorapp )。

我以前从未用任何语言做过这件事,我只是按照我的供应商告诉我他们做过的事情去做。显然他们以前从未处理过 PHP,也不知道该怎么做。

最佳答案

像这样:

header("Location: http://www.example.com/")

但它必须在任何其他代码之前...请参阅 php.net

关于java - PHP 中的服务器端重定向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1977925/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com