首页 > 编程技术 > php

php生成html文件代码

发布时间:2016-11-25 16:30

php生成html文件代码有很多种方法,比较常用的一种是为静态的使用方法,但我们下面使用的是直接生成纯html文件的php代码与方法

 ,下面我们来看看

第一种方法:就是用smary的模板来生成.

require('smarty/Smarty.class.php');
$t = new Smarty;
$t->assign("title","Hello World!");
$content = $t->fetch("templates/index.htm");
//这里的 fetch() 就是获取输出内容的函数,现在$content变量里面,就是要显示的内容了
$fp = fopen("archives/2005/05/19/0001.html", "w");
fwrite($fp, $content);
fclose($fp);
?>

另一种方法是用php od_get_contents来生成
ob_start();
echo "Hello World!";
$content = ob_get_contents();//取得php页面输出的全部内容
$fp = fopen("archives/2005/05/19/0001.html", "w");
fwrite($fp, $content);
fclose($fp);

 第三种方法就是用php自然的函数fopen来直接保存.

php生成验证码文件是一项学php的朋友都应该知道的技术哦,这个不既可以让你的网站安全提高一些,也可以注止一些机器注册之类的问题哦,下面我们就来看一个简单的生成验证码程序

php生成验证码文件是一项学php的朋友都应该知道的技术哦,这个不既可以让你的网站安全提高一些,也可以注止一些机器注册之类的问题哦,下面我们就来看一个简单的生成验证码程序


<?
/ eckNum.php
session_start();
function random(){
$srcstr="ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
mt_srand();
$strs="";
for($i=0;$i<6;$i++){
$strs.=$srcstr[mt_rand(0,35)];
}
return strtoupper($strs);
}
$str=random(4); //随就去干机生成的字符串
$width = 50; //验证码图片的宽度
$height = 25; //验证码图片的高度
@header("Content-Type:image/png");
$_SESSION["code"] = $str;
//echo $str;
$im=imagecreate($width,$height);
//背景色
$back=imagecolorallocate($im,0xFF,0xFF,0xFF);
//模糊97xxoo点颜色
$pix=imagecolorallocate($im,187,230,247);
//字体色
$font=imagecolorallocate($im,41,163,238);
//绘模糊作用的点
mt_srand();
for($i=0;$i<1000;$i++)
{
imagesetpixel($im,mt_rand(0,$width),mt_rand(0,$height),$pix);
}
imagestring($im, 5, 7, 5,$str, $font);
imagerectangle($im,0,0,$width-1,$height-1,$font);
imagepng($im);
imagedestroy($im);
$_SESSION["code"] = $str;
?> 

过滤器验证ip地址,是否合法。

定义和用法
该过滤器验证FILTER_VALIDATE_IP价值作为一个网址。

名称: “ validate_ip ”
身份证号码: 275
可能的旗帜:

<?php
$ip = "192.168.0.1";
if(!filter_var($ip, FILTER_VALIDATE_IP))
 {
 echo "IP is not valid";
 }
else
 {
 echo "IP is valid";
 }
?>
输出结果。
is is valid
很多朋友想用php或js获取客户端的MAC地址,我可告诉你这是不可能的,除网友权限设置非常底。

class GetMacAddr{   
  
       var $return_array = array(); // 返回带有MAC地址的字串数组   
       var $mac_addr;   
  
       function GetMacAddr($os_type){   
            switch ( strtolower($os_type) ){   
                     case "linux":   
                               $this->forLinux();   
                               break;   
                     case "solaris":   
                               break;   
                     case "unix":   
                               break;   
                     case "aix":   
                               break;   
                     default:   
                               $this->forWindows();   
                               break;   
  
            }   
  
              
            $temp_array = array();   
            foreach ( $this->return_array as $value ){   
  
                      if (   
preg_match("/[0-9a-f][0-9a-f][:-]"."[0-9a-f][0-9a-f][:-]"."[0-9a-f][0-9a-f][:-]"."[0-9a-f][0-9a-f][:-]"."[0-9a-f][0-9a-f][:-]"."[0-9a-f][0-9a-f]/i",$value,   
$temp_array ) ){   
                               $this->mac_addr = $temp_array[0];   
                               break;   
                     }   
  
            }   
            unset($temp_array);   
            return $this->mac_addr;   
       }   
  
  
       function forWindows(){   
            @exec("ipconfig /all", $this->return_array);   
            if ( $this->return_array )   
                     return $this->return_array;   
            else{   
                     $ipconfig = $_SERVER["WINDIR"]."system32ipconfig.exe";   
                     if ( is_file($ipconfig) )   
                        @exec($ipconfig." /all", $this->return_array);   
                     else  
                        @exec($_SERVER["WINDIR"]."systemipconfig.exe /all", $this->return_array);   
                     return $this->return_array;   
            }   
       }   
  
  
  
       function forLinux(){   
            @exec("ifconfig -a", $this->return_array);   
            return $this->return_array;   
       }   
  
}   
//方法使用   
//$mac = new GetMacAddr(PHP_OS);   
//echo $mac->mac_addr;   

PHP and AJAX Suggest

这个suggest是仿google的效果了,google总是走在世界的技术的前面啊,下面我们也来看看实例吧.

先看看html文件.

<form> 
First Name:
<input type="text" id="txt1"
onkeyup="showHint(this.value)">
</form>
<p>Suggestions: <span id="txtHint"></span></p>
下面用js文件的ajax文件来处理数据.
var xmlHttp;

function showHint(str)
{
if (str.length==0)
  { 
  document.getElementById("txtHint").innerHTML="";
  return;
  }
xmlHttp=GetXmlHttpObject();
if (xmlHttp==null)
  {
  alert ("Browser does not support HTTP Request");
  return;
  } 
var url="gethint.php";
url=url+"?q="+str;
url=url+"&sid="+Math.random();
xmlHttp.onreadystatechange=stateChanged;
xmlHttp.open("GET",url,true);
xmlHttp.send(null);
} 

function stateChanged() 
{ 
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
 { 
 document.getElementById("txtHint").innerHTML=xmlHttp.responseText;
 } 
}
function GetXmlHttpObject()
{
var xmlHttp=null;
try
 {
 // Firefox, Opera 8.0+, Safari
 xmlHttp=new XMLHttpRequest();
 }
catch (e)
 {
 // Internet Explorer
 try
  {
  xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
  }
 catch (e)
  {
  xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
 }
return xmlHttp;
}
 
这个文件只是返回php处理的数据,而我们的php文件如下.
<?php
// Fill up array with names
$a[]="Anna";
$a[]="Brittany";
$a[]="Cinderella";
$a[]="Diana";
$a[]="Eva";
$a[]="Fiona";
$a[]="Gunda";
$a[]="Hege";
$a[]="Inga";
$a[]="Johanna";
$a[]="Kitty";
$a[]="Linda";
$a[]="Nina";
$a[]="Ophelia";
$a[]="Petunia";
$a[]="Amanda";
$a[]="Raquel";
$a[]="Cindy";
$a[]="Doris";
$a[]="Eve";
$a[]="Evita";
$a[]="Sunniva";
$a[]="Tove";
$a[]="Unni";
$a[]="Violet";
$a[]="Liza";
$a[]="Elizabeth";
$a[]="Ellen";
$a[]="Wenche";
$a[]="Vicky";
//get the q parameter from URL
$q=$_GET["q"];
//lookup all hints from array if length of q>0
if (strlen($q) > 0)
{
$hint="";
for($i=0; $i<count($a); $i++)
  {
  if (strtolower($q)==strtolower(substr($a[$i],0,strlen($q))))
    {
    if ($hint=="")
      {
      $hint=$a[$i];
      }
    else
      {
      $hint=$hint." , ".$a[$i];
      }
    }
  }
}

//Set output to "no suggestion" if no hint were found
//or to the correct values
if ($hint == "")
{
$response="no suggestion";
}
else
{
$response=$hint;
}

//output the response
echo $response;
?>
好了我们的确suggest效果就完成了.
标签:[!--infotagslink--]

您可能感兴趣的文章: