2015-11-25 26 views
0

我有2個php文件。如何根據屏幕分辨率或大小執行php文件

include_once('medium.php'); 
include_once('mobile.php'); 

我想執行medium.php文件時,屏幕尺寸> 992px和mobile.php如果屏幕尺寸是< 992px

我試了一下,到目前爲止是:

<script> 
var width = $(window).width(); 
if (width > 992){ 
</script> 
include_once('medium.php'); 
<script>} else{</script> 
include_once('mobile.php'); 
<script>}</script> 

但無法獲得結果。

+9

[只在特定的屏幕分辨率包括PHP文件(的可能重複http://stackoverflow.com/questions/16172806/include-php-file - 僅在某些屏幕分辨率) – kayess

+0

這是如此錯誤,或者只是對我來說似乎不對 – madalinivascu

+0

更好的去與AJAX請求 – madalinivascu

回答

0

圍棋與jQuery和Ajax $.load()功能

var width = $(window).width(); 
if (width > 992){ 
    $("body").load("medium.php"); 
} else{ 
$("body").load("mobile.php"); 
} 
0

您應該使用Ajax或Http.get屏幕寬度到PHP文件,並讓PHP處理執行

<?php 
if(isset($_GET['size']) && $_GET['size'] > 992) { 
    include_once('medium.php'); 
} else { 
    include_once('mobile.php'); 
} ?> 
0

要麼使用jQuery.load()jQuery.post()jQuery.get()jQuery.ajax()

<script type="text/javascript"> 
var script = window.innerWidth > 992 ? "medium.php" : "mobile.php"; 
var postVars = {} // Use this to provide additional data to you PHP script, or ommit this parameter if you don't need it 
var dataType = 'json'; // The type of data your PHP script will return, could be html, json, text. ommit this if you don't have a particular use for it. 

jQuery.post(script, postVars, function(response) { 
    // This is your callback function, you can do whatever you please with response here. 
}, dataType) 
</script> 

安全通知 永遠不要相信最終用戶!由於這是客戶端腳本,這些變量可能會被操縱爲config.php。

爲了解決這個問題,您應該不是請求多個腳本,而是請求一個腳本,提供一個包含文件的參數,然後檢查是否允許包含該文件。

<script type="text/javascript"> 
var script = window.innerWidth > 992 ? "medium.php" : "mobile.php"; 
var postVars = {file_to_include: script} 
var dataType = 'json'; // The type of data your PHP script will return, could be html, json, text. ommit this if you don't have a particular use for it. 

jQuery.post("include.php", postVars, function(response) { 
    // This is your callback function, you can do whatever you please with response here. 
}, dataType) 
</script> 

然後在你的PHP腳本

<?php 
    if(in_array($_POST["file_to_include"], array("medium.php", "mobile.php"))) { 
     include $_POST["file_to_include"]; 
    } 
?>