[英]how to execute php file according to screen resolution or size
我有2個php文件。
include_once('medium.php');
include_once('mobile.php');
我想在屏幕尺寸> 992px時執行medium.php文件,而在屏幕尺寸<992px時執行mobile.php文件
到目前為止,我嘗試過的是:
<script>
var width = $(window).width();
if (width > 992){
</script>
include_once('medium.php');
<script>} else{</script>
include_once('mobile.php');
<script>}</script>
但無法獲得結果。
使用jquery和ajax $.load()
函數
var width = $(window).width();
if (width > 992){
$( "body" ).load( "medium.php" );
} else{
$( "body" ).load( "mobile.php" );
}
您應該使用Ajax或Http.get將寬度過濾到PHP文件,並讓php處理執行
<?php
if(isset($_GET['size']) && $_GET['size'] > 992) {
include_once('medium.php');
} else {
include_once('mobile.php');
} ?>
使用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"];
}
?>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.