gpt4 book ai didi

javascript - 我怎样才能以允许我使用 Ajax 在 .JS 中添加它的方式转换此 PHP 代码?

转载 作者:行者123 更新时间:2023-11-30 06:56:55 26 4
gpt4 key购买 nike

我在 domain1.com 上托管了一个 .js 文件,但要使其正常工作,我需要在开头添加 PHP 代码。这样做的原因是为了绕过 Safari 对我的脚本的一些限制,它需要我创建一个 session 。 PHP 代码通过指向 domain2.com 的 url 创建 session 。没有浏览器重定向或任何东西,用户停留在 domain1.com。我想在 domain1.com 中有一个 .js 文件,所以也许我需要一个 AJAX 解决方案。在这里:

<?php
session_start();

if (!isset($_SESSION['isIFrameSessionStarted']))
{
$_SESSION['isIFrameSessionStarted'] = 1;
$redirect = rawurlencode('http://' . "{$_SERVER['HTTP_HOST']}{$_SERVER['REQUEST_URI']}");
header('Location: domain2.com/start-session.php?redirect=' . $redirect);


exit;
}

?>

start-session.php 文件托管在 domain2.com 上不需要任何更改,它包含以下内容:

<?php
session_start(); // create the session cookie
$redirect = rawurldecode($_GET['redirect']);

header('Location: ' . $redirect); // redirect back to domain
exit;
?>

最佳答案

让我结合您在评论中要求的内容:

I have a .js file hosted on domain1 ... I want to have a single js file and I can't put PHP into that ... the whole purpose of this is for domain1 to not have any php code or php file. ... The reason is because I want it cross-domain and the session to be created from domain2.

听起来您的问题可能与 Safari iFrame session cookie problem 有关,特别是因为您的代码块之一中有 if (!isset($_SESSION['isIFrameSessionStarted']))。我将继续这个假设。

其他读者的问题总结:

Upon embeding an IFrame from one domain into a website of a different domain, you will quickly realise that Internet Explorer and Safari are blocking the cookies (and thus the session variables) of the website inside the IFrame (ref).


未成功的尝试解决方案:


我的解决方案:

本质上,PHP session “劫持”。在上述解决方案失败的情况下,它的效果出奇地好。这是基本 解决方案。请执行任何您喜欢的安全增强* 和 URL 美化。基本上,我们通过重定向检索 PHP session ID 并将其传递给 iframe。说明在评论中。

在你的 domainA.com 头部放置这个:

<script src="session.js"></script>

session.js(在 domainA.com 上):

// Location of the domain B session starter
var sessionScriptURL = "http://domainB.com/start-session.php";
var refQSparam = "phpsessionid";

// Check if we have the phpsessionid in the query string
var phpsessionid = getParameterByName(refQSparam);
if(phpsessionid === null) {
// Not in the query string, so check if we have it in session storage
var sessionStore = sessionStorage.getItem(refQSparam);
if(sessionStore === null) {
// We have no session storage of the PHP session ID either, redirect to get it
top.location = sessionScriptURL + "?redirect=" + encodeURIComponent(self.location.href);
} else {
// phpsessionid was found in session storage. Retrive it
phpsessionid = sessionStore;
}
} else {
// Save the phpsessionid to session storage for browser refresh
sessionStorage.setItem(refQSparam, phpsessionid);
// Optional: Redirect again to remove the extra query string data
}

// Helper to get QS values
function getParameterByName(name) {
return decodeURIComponent((new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(location.search)||[,""])[1].replace(/\+/g, '%20'))||null;
}

session-starter.php(在 domainB.com 上):

<?php
session_start(); // create the session cookie
$redirect = rawurldecode($_GET['redirect']);

// redirect back with the php session ID
// Optional: encode this information
$href = $redirect . '?phpsessionid=' . session_id();
header('Location: ' . $href);
exit;

HTML(在正文中,在 domainA.com 上):

将 PHP session 信息附加到 iframe src。

<script>
document.write('<iframe src="http://domainB.com/embedded-script.php?phpsessionid='+phpsessionid+'"></iframe>');
</script>

embedded-script.php(在 domainB.com 上,在 iframe 中):

<?php
// Use the phpsessionid passed in
$phpsessionid = rawurldecode($_GET['phpsessionid']);

// REF: http://php.net/manual/en/function.session-id.php
function session_valid_id($session_id) {
return preg_match('/^[-,a-zA-Z0-9]{1,128}$/', $session_id) > 0;
}
// Check that this is potentially a valid session ID
if(session_valid_id($phpsessionid)) {
// Set the session to the one obtained in session-start.php
session_id($phpsessionid);
}
session_start(); // Only call this after session_id()!
// Rest of your code

*注意事项:

  1. 不要实际使用 document.write,使用 jQuery 或文档选择器。

  2. 对 PHP session ID 进行编码

  3. 执行另一个重定向回 domainA.com 的基本 URL 以删除 URL 中的 ?phpsessionid= 以获得更清晰的外观。

  4. 如果您决定使用 AJAX 调用 session-starter.php,出于同样的原因,您每次都会获得一个 PHP session ID。 iframe 将成功使用此 session ID,但如果您打开 domainB.com 的新页面, session 将再次不同。

关于javascript - 我怎样才能以允许我使用 Ajax 在 .JS 中添加它的方式转换此 PHP 代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30613894/

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