首页 > 编程技术 > php

用PHP程序实现随机广告图片显示

发布时间:2016-11-25 17:35

以下为引用的内容:
<?php
#########随机广告显示##########
function myads(){
$dir="ads"; #设置存放记录的目录
//$dir="ads"; #设置存放记录的目录
$ads="$dir/ads.txt"; #设置广告代码文件
$log ="$dir/ads.log"; #设置ip记录文件

$ads_lines=file($ads);
$lines=count($ads_lines);#文件总行数

####读出广告总数$ads_count和显示次数到数组$display_array########
$ads_count=0;
$display_count=0;
for ($i=0;$i<$lines;$i ){
if((!strcmp(substr($ads_lines[$i],0,7),"display"))){
$ads_count =1;
$display_array[$ads_count]=substr($ads_lines[$i],8);
$display_count =$display_array[$ads_count];
}
}
####决定随机显示序号$display_rand#####
srand((double)microtime()*1000000);
$display_rand = rand(1,$display_count);

###决定广告序号$ads_num######
$pricount=0;
$ads_num=1;
for($i=1; $i<=$ads_count; $i ) {
$pricount = $display_array[$i];
if ($display_rand<=$pricount) {$ads_num=$i;break;}
}

#####播放广告########
$num=0;
$flag=0;

for($i=0;$i<$lines;$i ){
if((!strcmp(substr($ads_lines[$i],0,7),"display"))){$num ;}
if(($num==$ads_num)and($flag==0)){$flag=1;continue;}
if(($flag==1)and strcmp($ads_lines[$i][0],"#")){echo $ads_lines[$i];continue;}
if(($flag==1)and(!(strcmp($ads_lines[$i][0],"#")))){break;}
}
####纪录广告显示次数#########
$fp=fopen($log,"a");
fputs($fp,date( "Y-m-d H:i:s " ).getenv("REMOTE_ADDR")."==>".$ads_num."n");
fclose($fp);
}
?>


广告代码文件ads.txt
以下为引用的内容:
########每个广告代码之间用'#'隔开,display为显示加权数,越大显示次数越多######
display=10
<a href="广告1连接地址">
<img" width=100% src="/images/banner/webjxcomad1.gif" alt="广告1"> </a>
################################
display=10
<a href="广告2连接地址" target=_blank>
<img" width=100% src="/images/banner/webjxcomad2.gif" width="468" height="60" alt="广告2" border="0"></a>


调用<?php myads();?>即可

综述:有许多朋友对网站提供繁、简两种版本感到很困惑,是怎么实现的呢?这也是时下众多PHP书籍中被漏掉的一个很重要的知识点。笔者搜集整理并根据自己的开发经验将一些重点与疑点罗列出来与大家共享!

  如何应用繁体中文转换为简体中文的PHP函数<?

  我们定义一个big5togb的函数来实现这个转换:

function big5togb($code)
{
//参数$code是big5码的字符串
include "data_big5.php"; //包含big5数据的文件
$output="";
$length=strlen($code); //取得字符串长度
$code=strtok($code,"");
$idx=0;
while ($idx < $length)
{
$tmpStr=$code[$idx].$code[$idx 1];

if (isbig5($tmpStr)) //判定是否big5码
{
……//假如是big5码则进行转换后输出
}
else
{
$output.= $code[$idx]; //假如不是big5码则直接输出
}
$idx ;
}
return ($output);
}

  如何应用简体中文转换为繁体中文的PHP函数?

  如何用PHP将简体中文转换为繁体中文?

  我们定义一个big5togb的函数来实现这个转换:

function gbtobig5($code)
{
include "data_gb.php"; //包含有gb码的数据文件
$output="";
$length=strlen($code);
$code=strtok($code,"");
$idx=0;
while ($idx < $length)
{
$tmpStr=$code[$idx].$code[$idx 1];

if (isgb($tmpStr)) //判定是否gb码
{
……//假如是gb码转换后输出
}
else
{
$output.= $code[$idx]; //不是gb码则直接输出
}
$idx ;
}
return ($output);
}
  在简繁体转换中怎样应用PHP输出控制功能?

  PHP输出控制功能是怎样一回事?

  PHP的输出信息控制函数可以让你控制你的脚本输出的内容,可以用于许多不同的情况,非凡是在你的脚本已经输出信息后需要发送文件头的情况以及需要对输出信息进行编辑处理的地方。输出控制函数不对使用header()或setcookie()发送的文件头信息产生影响,只对那些类似于echo()、print() 和 PHP 代码的数据块有作用。

  例 1. 控制输出

test.php
<?
function test($str){
return str_replace("world","php",$str);
}
ob_start("test");
echo "hello world";
ob_end_flush();
?>

<?php
class CreateHtml
{
function mkdir( $prefix= 'article' )
{
$y = date('Y');
$m = date('m');
$d = date('d');
$p=DIRECTORY_SEPARATOR;
$filePath='article'.$p.$y.$p.$m.$p.$d;
$a=explode($p,$filePath);
foreach ( $a as $dir)
{
$path.=$dir.$p;
if(!is_dir($path))
{
//echo '没有这个目录'.$path;
mkdir($path,0755);
}
}
return $filePath.$p;
}
function start()
{
ob_start();
}
function end()
{
$info = ob_get_contents();
$fileId = '12345';
$postfix = '.html';
$path = $this->mkdir($prefix= 'article');
$fileName = time().'_'.$fileId.$postfix;
$file=fopen($path.$fileName,'w ');
fwrite($file,$info);
fclose($file);
ob_end_flush();
}
}
?>
<?php
$s=new CreateHtml();
$s->start();
?>
<html>
<body>
asdfasdfasdfasdfasdfasdfasdfasdfasdf<br>
adfasdfasdf<br>
</body>>
</html>
<?php
$s->end();
?>

<?php

$img_array = array();

$content1 = stripslashes($content1);

if (get_magic_quotes_gpc()) $content1 = stripslashes($content1);

//echo $content1;

preg_match_all("/(src│SRC)="(http://(. ).(gif│jpg│jpeg│bmp│png))/isU",$content1,$img_array);

$img_array = array_unique(dhtmlspecialchars($img_array[2]));

set_time_limit(0);

foreach ($img_array as $key => $value) {

$get_file = file_get_contents($value);

$filetime = time();

$filepath = "../uploadfile/".date("Y",$filetime)."/".date("m",$filetime)."/";

!is_dir($filepath) ? mkdirs($filepath) : null;

$filename = date("YmdHis",$filetime).random(1).'.'.substr($value,-3,3);

$fp = @fopen($filepath.$filename,"w");

@fwrite($fp,$get_file);

fclose($fp);

$content1 = preg_replace("/".addcslashes($value,"/")."/isU", "/uploadfile/".date("Y",$filetime)."/".date("m",$filetime)."/".$filename, $content1);

?>

经常会有人问模拟登陆的问题,其实原理很简单,只要把SessionID保存下来就可以了,今天花了一个小时的时间写了一个函数,供大家参考,网站返回的头信息,具体网站具体分析。

  源代码:

<?php
/*
* 得到网页内容
* 参数:$host [in] string
* 主机名称(例如: www.etoow.com)
* 参数:$method [in] string
* 提交方法:POST, GET, HEAD ... 并加上相应的参数( 具体语法参见 RFC1945,RFC2068 )
* 参数:$str [in] string
* 提交的内容
* 参数:$sessid [in] string
* PHP的SESSIONID
*
* @返回 网页内容 string
*/
function GetWebContent($host, $method, $str, $sessid = '')
{
$ip = gethostbyname($host);
$fp = fsockopen($ip, 80);
if (!$fp) return;
fputs($fp, "$methodrn");
fputs($fp, "Host: $hostrn");
if (!empty($sessid))
{
fputs($fp, "Cookie: PHPSESSID=$sessid; path=/;rn");
}
if ( substr(trim($method),0, 4) == "POST")
{
fputs($fp, "Content-Length: ". strlen($str) . "rn"); // 别忘了指定长度
}
fputs($fp, "Content-Type: application/x-www-form-urlencodedrnrn");
if ( substr(trim($method),0, 4) == "POST")
{
fputs($fp, $str."rn");
}
while(!feof($fp))
{
$response .= fgets($fp, 1024);
}
$hlen = strpos($response," "); // LINUX下是 " "
$header = substr($response, 0, $hlen);
$entity = substr($response, $hlen 4);
if ( preg_match('/PHPSESSID=([0-9a-z] );/i', $header, $matches))
{
$a['sessid'] = $matches[1];
}
if ( preg_match('/Location: ([0-9a-z_?=&#.] )/i', $header, $matches))
{
$a['location'] = $matches[1];
}
$a['content'] = $entity;
fclose($fp);
return $a;
}

/* 构造用户名,密码字符串 */
$str = ("username=test&password=test");
$response = GetWebContent("localhost","POST /login.php HTTP/1.0", $str);
echo $response['location'].$response['content']."<br>";
echo $response['sessid']."<br>";
if ( preg_match('/error.php/i',$response['location']))
{
echo "登陆失败<br>";
} else {
echo "登陆成功<br>";
// 不可以访问user.php,因为不带sessid参数
$response = GetWebContent("localhost","GET /user.php HTTP/1.0", '', '');
echo $response['location']."<br>"; // 结果:error.php?errcode=2

// 可以访问user.php
$response = GetWebContent("localhost","GET /user.php HTTP/1.0", '', $response['sessid']);
echo $response['location']."<br>"; // 结果:user.php
}
?>

标签:[!--infotagslink--]

您可能感兴趣的文章: