简体   繁体   English

PHP中的服务器端重定向

[英]Server-Side Redirect in PHP

I have a Java application that I need to integrate our existing PHP website with. 我有一个Java应用程序,需要与我们现有的PHP网站集成。 The vendor wants us to do a server-side redirect to allow for secure authentication and single-sign-on, but I'm not sure how to do that in PHP. 供应商希望我们进行服务器端重定向以允许安全身份验证和单点登录,但是我不确定如何在PHP中执行此操作。 The vendor explained the workflow as follows: 供应商对工作流程的解释如下:

  1. User clicks on a 'Open Application' link on our PHP site 用户点击我们PHP网站上的“打开应用程序”链接
  2. The PHP application hits a page on the Java application, sending the authentication parameters PHP应用程序在Java应用程序上访问页面,发送身份验证参数
  3. If successful, the PHP application sends the headers back to the user's browser, which forces a 'redirect', otherwise the PHP app displays an error 如果成功,PHP应用程序会将标头发送回用户的浏览器,这将强制执行“重定向”,否则PHP应用程序将显示错误

What this will allow would be for our PHP app to securely talk to the Java app, and the client never has to send any sort of authentication. 这将允许我们的PHP应用程序与Java应用程序安全地进行通信,并且客户端无需发送任何形式的身份验证。

From what I understand, .NET and Java have this capability built in, but I can't find a way in PHP to do this. 据我了解,.NET和Java都内置了此功能,但是我找不到在PHP中实现此功能的方法。 Any ideas? 有任何想法吗?

UPDATE 更新

I'm not talking about using the header("Location: ..."); 我不是在谈论使用header(“ Location:...”); function to do a redirect. 功能进行重定向。 The kicker with this server-side redirect is that the app does the authentication and sends all that information back to the client so that the client is then logged in. Using header("Location: ...") just forces the browser to go elsewhere. 服务器端重定向的主要作用是应用程序执行身份验证并将所有信息发送回客户端,以便客户端随后登录。使用header(“ Location:...”)只会强制浏览器运行别处。

UPDATE 2 更新2

autologin.php (Simulates the user logging into an external app via curl) 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 (Contains the 'login' form) 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 (Restricted page) strict.php(受限页面)

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

The idea is that the user wants to ultimately get to 'restricted.php'. 这个想法是用户希望最终进入“ restricted.php”。 'login.php' contains the code necessary to log in. What I want to simulate is the user filling out the form on 'login.php' and logging the user into 'restricted.php'. “ login.php”包含登录所必需的代码。我要模拟的是用户填写“ login.php”上的表单并将用户登录到“ restricted.php”。

The above snippets of code work together on my local tests (hitting autologin.php redirects to restricted.php and the secret token is printed out), but I can't seem to get it to work cross-application. 上面的代码片段在我的本地测试中一起工作(点击autologin.php重定向到stricted.php并打印出秘密令牌),但是我似乎无法使其跨应用程序工作。 The apps will be on the same domain ( https://domain.com/myapp , https://domain.com:1234/vendorapp ). 该应用程序将在同一个域( https://domain.com/myapphttps://domain.com:1234/vendorapp )。

I've never done this before in any language, I'm just going off of what my vendor has told me they've done. 我从来没有用任何一种语言来做过此事,我只是从供应商告诉我他们已经做过的事情开始。 Apparently they've never dealt with PHP before and have no idea what to do. 显然,他们以前从未处理过PHP,也不知道该怎么办。

like this: 像这样:

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

But it must come before any other code...see php.net 但是它必须在任何其他代码之前...参见php.net

You just output a normal HTTP redirect header() like this: 您只需输出一个普通的HTTP重定向header()如下所示:

<?php header('Location: http://www.example.com/'); ?>

Re Update 重新更新

If I understand correctly you'd need to do this: 如果我正确理解,则需要执行以下操作:

  1. Browser POSTs login request to PHP server 浏览器POST将请求登录到PHP服务器
  2. PHP script packages the login information in some specific form for JSP app PHP脚本以某种特定形式打包了JSP应用程序的登录信息
  3. PHP script POSTs (via cURL ) or SOAPs or whatever is necessary to JSP app PHP脚本POST(通过cURL )或SOAP或JSP应用程序所需的任何东西
  4. PHP receives the response and parses out the necessary information PHP收到响应并解析出必要的信息
  5. PHP sends header and/or body data back to browser PHP将标头和/或正文数据发送回浏览器

Step 4, parsing the information, depends on how you send and receive the information. 步骤4,解析信息,取决于您如何发送和接收信息。 If you receive them in the header via cURL, you'll need to set CURLOPT_HEADER to true and parse the necessary data out of the response. 如果通过cURL在标头中接收到它们,则需要 CURLOPT_HEADERtrue并从响应中解析出必要的数据。 This may be as simple as splitting the string on the first blank line or more complicated, that depends on your specific situation. 这可能像在第一行空白处分割字符串一样简单,也可能更复杂,具体取决于您的具体情况。

How this logs in the user in your app is something you need to handle as well. 还需要处理如何在应用程序中登录用户。 The JSP app probably handles the actual password and username and hands you back a token of some sort which you'll need to keep track of. JSP应用程序可能会处理实际的密码和用户名,并将您需要跟踪的某种令牌递给您。

It sounds like you are looking for the curl library, which is usually bundled with PHP. 听起来您正在寻找curl库,该库通常与PHP捆绑在一起。

http://php.net/manual/en/book.curl.php http://php.net/manual/zh/book.curl.php

<?php
session_start();

// Receive username / password from $_POST

// Prepare CURL object for post

// Post u/p to java server

// Read response

if($success)
{
    header('Location: nextpage.php');
    $_SESSION['LoggedInTime'] = time();
    exit;
} 
else
{
    //display error
}

Update: 更新:

Later, you can check $_SESSION['LoggedInTime'] + 3600 > time() to see if they are still logged in. Every time they visit a logged in page, do this: 稍后,您可以检查$_SESSION['LoggedInTime'] + 3600 > time()以查看他们是否仍在登录。每次他们访问登录页面时,请执行以下操作:

if($_SESSION['LoggedInTime'] + 3600 > time()) 
{
     $_SESSION['LoggedInTime'] = time() + 3600;
} 
else
{
     header('Location: /login.php?Message=session+expired');
     exit;
}

Hope this helps. 希望这可以帮助。

If you are trying to integrate php and java on the web, you may want to look into Quercus/Resin. 如果您想在网络上集成php和java,则可能需要研究Quercus / Resin。 Your PHP can then call java code directly. 然后,您的PHP可以直接调用Java代码。 Since they are running on the same server, the java code could write any cookies, setup any sessions or doing any necessary setup processing. 由于它们在同一服务器上运行,因此Java代码可以编写任何cookie,设置任何会话或进行任何必要的设置处理。 http://www.caucho.com/resin-3.0/quercus/tutorial/module/index.xtp http://www.caucho.com/resin-3.0/quercus/tutorial/module/index.xtp

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

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