我正在设置一个本地网页,用于显示HTML5视频标记中的视频。我只是希望能够从PHP请求进行数据库搜索,并显示我可以点击的结果并显示我想要的视频。我遇到的问题是,当从“file:///”链接加载而不是从“http://”链接加载时,视频加载速度更快。服务器在“HTTP”模式下完美运行但在“file:///”模式下无效,这是正常的,因为PHP代码仅在请求服务器时在服务器端执行。
我花了一整天的时间尝试了很多东西。我改变了我的服务器以接受CORS,我尝试了window.open,将引用存储在变量,本地或全局中,但是一旦我离开了我的javascript函数,我就失去了它。我在一个从另一个函数调用的函数中尝试了window.open,但无论我做什么,一旦我离开函数,或者函数完成后,窗口引用就会丢失。由于我的浏览器被用作我的主浏览器,我不想禁用安全arround CORS,但由于我的网页链接来自“file:///”在同一台计算机上请求“HTTP”,因此CORS阻止我并想要一个HTTP请求,我不能给。
我已经完成了从其他网页检索信息的所有搜索,但我总是遇到“同一域”问题。我尝试了AJAX HTTPRequest,我对这个简单的问题没有更多的解决方案,这个问题比预期的要复杂得多。最初的问题只是我的视频在HTTP模式下加载速度不够快(速度差异极端,10分钟视频,我可以等待5-10秒跳过它,而在FILE:///网址中,它几乎是即时的没有等待。更长的视频1小时,我可以等待20秒和30秒,而在文件:///模式,几乎是即时的。)我必须学习所有允许跨域的东西,最终没有成功。我想,也许其他几个脑袋可能比我现在有更好的想法。
#In my httpd.conf file from Apache
DocumentRoot "e:/mainwebfolder"
Alias "/lp" "d:/whatever"
//////////////////////////////////////
// index.php file that does not contain PHP contents
// window.location.href: file://d:/whatever/index.php
//////////////////////////////////////
<head>
<script src="html/servcom.js" type="text/javascript"></script>
</head>
<video id="vplayer" width="1280" height="720" controls></video>
<div id="search-form">
<input id="srch" name="srch" type="text">
<button class="bbut" onclick="ServInfo('search-results','http://127.0.0.1/lp/html/db.php','mode=s','search-form');">Search</button>
</div>
<div id='search-results'></div>
<script>
var dplay = document.getElementById("vplayer");
ShowVideo('MyVideo.mp4');
function ShowVideo (vidUrl) {
dplay = document.getElementById("vplayer");
dplay.src = vidUrl;
dplay.load;
}
</script>
//////////////////////////////////////
// Now this is in my javascript file servcom.js
//////////////////////////////////////
var win_ref = -1;
function ServInfo(pop_field_id,web_page,params="",form_id="",exec_string = "") {
var sparams = params;
var swpage = web_page;
var eobj = document.getElementById(pop_field_id);
var moreparams = "";
// If we entered extra parameters including form fields,
// add the the "&" before the form field list
if (sparams != "") {moreparams = "&";}
// Get form field values if a form id is specified
if (form_id != "") {
var efrm = document.getElementById(form_id);
sparams += moreparams+GetDivFields(form_id);
}
// Add the question mark if there is any parameters to pass
if (sparams != "") {
sparams = "?"+sparams;
// Add recieving objects reference
sparams += "&srco="+pop_field_id;
}
// If HTML element to populate does not exist, exit
if (typeof(eobj) == "!undefined" || eobj == null) {return;}
win_ref = window.open(swpage+sparams,"_blank");
//////////////////////////////////////
// right here win_ref will never be available once the code from this function has been finished executing although the variable is global. The problem starts here.
//////////////////////////////////////
// Execute a string if a user defined one
if (exec_string != "") {eval(exec_string);}
}
// Build a parameter string with div fields of type text, hidden or password
function GetDivFields(div_id) {
var ediv = document.getElementById(div_id);
var elem = ediv.children;
var retval = "";
var ssep = "";
for (var i = 0; i < elem.length; i++) {
if (elem[i].type == "text" || elem[i].type == "hidden" || elem[i].type == "password") {
retval += ssep+elem[i].name+"="+pURL(elem[i].value);
ssep = "&";
}
if (elem[i].type == "checkbox") {
if (elem[i].checked == true) {
retval += ssep+elem[i].name+"="+elem[i].value;
ssep = "&";
}
}
}
return retval;
}
//////////////////////////////////////
// And this is a brief overview of my db.php page
//////////////////////////////////////
<?php // Search Database code ?>
<div id="output"></div>
<script>
document.getElementById('output').innerHTML = "<?php echo $search_results; ?>";
// I actually want to retrieve the info from this div element once it has been populated from the initial page that called window.open for this page. BUT again. window.opener becomes empty once my initial window.open script finishes.
</script>
访问我新加载的页面的“输出”div innerHTML或通过本地HTTP加载视频,速度与“FILE:///”一样快。
好吧,我勉强找到了解决方案。由于这仅供本地和演示使用,我可以绕过一些证券。基本上,做我们通常不会在网站上做的事情,但所有这些都没有修改您的网络服务器配置或触摸任何.htaccess文件。基本上,没有安全限制,只是一个普通的黑客攻击,不会对您的浏览器或服务器造成安全漏洞。
需要注意的是:
PHP可以在我决定绕过CORS的服务器上写文件。因为通过AJAX的XML请求可以在同一个源域上完成,因此纯粹是在javascript中。如果存在不包含PHP代码的文件并且驻留在相同的域名i / e“file:///”上,则可以在没有任何问题的情况下读取内容。
所以我只是在我的db.php文件中执行以下操作:
$s_mode = "";
$s_text = "";
$sres = "";
if (isset($_REQUEST["srch"])) {$s_text=$_REQUEST["srch"];}
if (isset($_REQUEST["mode"])) {$s_mode=$_REQUEST["mode"];}
if ($s_mode == "s") {
$sres = SearchDB($s_text);
WriteFile("D:/whatever/my_path/dbres.html",$sres);
}
// Writes the contents of the search in a specified file
function WriteFile($faddress,$fcontents) {
$ifile = fopen($faddress,"w");
fwrite($ifile,$fcontents);
fclose($ifile);
}
现在使用普通的AJAX请求,我做了两件事。我选择使用带有“display:none”样式的iframe,以免看到另一个标签打开。
我在servcom.js中添加了一个新函数。所以我的新文件的内容如下所示:
http://127.0.0.1/whatever/db.php?param1=data&parma2=data
现在,我的dbres.html文件将只包含div元素以及我需要在我的“file:///”页面中显示的所有信息,搜索请求来自该页面。所以我只是在我的页面中有这个:
// Show page info in another page element or window with parameters (for local use only)
function ServInfoLocal(dest_frame,web_page,params="",form_id="") {
var sparams = params;
var swpage = web_page;
var iweb = document.getElementById(dest_frame);
var moreparams = "";
// If we entered extra parameters including form fields,
// add the the "&" before the form field list
if (sparams != "") {moreparams = "&";}
// Get form field values if a form id is specified
if (form_id != "") {
var efrm = document.getElementById(form_id);
sparams += moreparams+GetDivFields(form_id);
}
// If destination frame does not exist, exit
if (typeof(iweb) == "!undefined" || iweb == null) {return;}
// Add the question mark if there is any parameters to pass
if (sparams != "") {sparams = "?"+sparams;}
// Show results in iframe
iweb.src = swpage+sparams;
}
// AJAX simple HTTP GET request
function ServInfo(pop_field_id,web_page,params="",form_id="",append_data_to_output = "",exec_string = "",dont_show_results = "") {
var sparams = params;
var swpage = web_page;
var eobj = document.getElementById(pop_field_id);
var moreparams = "";
// If we entered extra parameters including form fields,
// add the the "&" before the form field list
if (sparams != "") {moreparams = "&";}
// Get form field values if a form id is specified
if (form_id != "") {
var efrm = document.getElementById(form_id);
sparams += moreparams+GetDivFields(form_id);
}
// If HTML element to populate does not exist, exit
if (typeof(eobj) == "!undefined" || eobj == null) {return;}
if (window.XMLHttpRequest) {
// IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
}
else {
// IE6-
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
// Do not show any results if requested
if (dont_show_results == "") {
if (append_data_to_output == "y") {
document.getElementById(pop_field_id).innerHTML += this.responseText;
}
if (append_data_to_output == "") {
document.getElementById(pop_field_id).innerHTML = this.responseText;
}
}
// Execute a string if a user defined one
if (exec_string != "") {
eval(exec_string);
}
}
};
// Add the question mark if there is any parameters to pass
if (sparams != "") {swpage += "?";}
xmlhttp.open("GET",swpage+sparams,true);
xmlhttp.send();
}
// Build a parameter string with div fields of type text, hidden or password
function GetDivFields(div_id) {
var ediv = document.getElementById(div_id);
var elem = ediv.children;
var retval = "";
var ssep = "";
for (var i = 0; i < elem.length; i++) {
if (elem[i].type == "text" || elem[i].type == "hidden" || elem[i].type == "password") {
retval += ssep+elem[i].name+"="+pURL(elem[i].value);
ssep = "&";
}
if (elem[i].type == "checkbox") {
if (elem[i].checked == true) {
retval += ssep+elem[i].name+"="+elem[i].value;
ssep = "&";
}
}
}
return retval;
}
现在我有2个按钮,一个用于搜索,一个用于显示我新创建的文件的结果。现在,我可以显示我的本地视频,它将直接加载到我的视频容器中,并带有“file:///”源,而不会通过http。我将自动显示结果,我可以从这里开始自己做。
因此,如果地球上的某个人希望能够从直接从Windows资源管理器运行的本地文件中在MySQL数据库中进行跨域搜索,那么解决方案并不是太多,实际上,我找不到任何解决方案,所以这里至少有一个谁会需要这个解决方案。
对于好奇的那些,我的下一步将循环我的文件夹,直到我的dbres文件使用另一个js函数。一旦我的文件被提取,调用另一个将破坏创建的文件的php文件,我将准备好从位于“file:///”位置的我的网页发出另一个数据库请求。