|
 |
|
รบกวนแนะนำการเขียน Random แบนเนอร์ โดยส่งค่ามาจากคนละ โดเมนครับ |
|
 |
|
|
 |
 |
|
ไฟล์ index.html
สมมติ ว่า ไฟล์นี้จะเก็บไว้ โดเมน A
Code (PHP)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script type="text/javascript" src="jquery-1.7.1.min.js"></script>
<script type="text/javascript">
jQuery(document).ready(function(jQuery){
var t = new Date().getTime();
function get_image(width, height, callback){
jQuery.ajax({
dataType: 'jsonp',
type: 'GET',
data: {'w':width, 'h':height, 't':t},
cache: false,
jsonp: 'jsonp_callback',
url: "http://www.xxx.com/img.php",
success: callback
});
}
jQuery.each({250:250, 468:60, 728:90}, function(width, height){
get_image(width, height, function(response){
jQuery('#add_'+width+' a').attr('href', response.url);
jQuery('#add_'+width+' img').attr('src', response.img);
jQuery('#add_'+width+' img').attr('alt', response.alt);
})
});
});
</script>
</head>
<body>
<div id="add_250"><a href="" target="_blank"><img src="" /></a></div>
<div id="add_468"><a href="" target="_blank"><img src="" /></a></div>
<div id="add_728"><a href="" target="_blank"><img src="" /></a></div>
</body>
</html>
ไฟล์ img.php
ไฟล์นี้จะเก็บไว้ที๋โดเมน xxx
Code (PHP)
<?php
function randomNumber($min, $max, $quantity) {
$numbers = range($min, $max);
shuffle($numbers);
return array_slice($numbers, 0, $quantity);
}
$ads = array(
array(
'img' => 'banner1',
'alt' => 'www.banner1.com'
),
array(
'img' => 'banner2',
'alt' => 'www.banner2.info'
),
array(
'img' => 'banner3',
'alt' => 'www.banner3.com'
),
array(
'img' => 'banner4',
'alt' => 'www.banner4.com'
),
array(
'img' => 'banner5',
'alt' => 'www.banner5.com'
),
array(
'img' => 'banner6',
'alt' => 'www.banner6.com'
)
);
$num = randomNumber(0,9,1);
$img = array(
'img' => 'http://'.$_SERVER['HTTP_HOST'].'/img-path/'.$_GET['w'].'x'.$_GET['h'].'/'.$ads[$num[0]]['img'].'-'.$_GET['w'].'x'.$_GET['h'].'.jpg',
'url' => 'http://'.$ads[$num[0]]['alt'].'.com',
'alt' => $ads[$num[0]]['alt']
);
echo $_GET['jsonp_callback'] . '(' . json_encode($img) . ');';
unset($img);
unset($num);
exit();
?>
การทำงานมันก็ปกติ ครับ แต่ที่มีปัญหาคือ ทาง โฮสแจ้งมาว่า สคริปที่ผมเขียนมันกิน CPU เกิน 25 % เลยอยากรบกวน
แนะนำ ปรับโค้ดของผม หรือวิธีเขียนที่มันจะไม่กิน CPU ของทางโฮสเขาครับ
ขอบคุณครับ
Tag : PHP

|
|
 |
 |
 |
 |
Date :
2013-05-25 23:45:19 |
By :
WiTT |
View :
865 |
Reply :
3 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ดันนิด ๆ
|
 |
 |
 |
 |
Date :
2013-05-26 21:41:26 |
By :
WiTT |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ลองเปลี่ยนจากการเขียน array of array เป็นการเขียน array of string ไปเลยดีกว่าครับ
|
 |
 |
 |
 |
Date :
2013-05-26 21:52:26 |
By :
itpcc |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Code (PHP)
array('img'=>'xx', 'alt'=>'yyy');
อย่างนี้เลยหรือคครับ
|
 |
 |
 |
 |
Date :
2013-05-26 22:36:53 |
By :
WiTT |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|
|

|
Load balance : Server 01
|