php判断远程文章是否存在程序代码
今天突发奇想, 我在给一客户写一案例时,有一个地方要求网友填写url但是,要保存是正确可访问的,虽然客户没要求但是我们还是想到了这样做,就是用php 的file_get_contents ,ob_get_contents() 来读取远程文章内容,如果有就真否则就假了,下面是我举的两个实例了.关于ob_get_contents() 我们要在php.ini里修改几个地方curl_开头的地方前面的;去了就OK了.
<?php
$url='http://www.111cn.net';
if(@file_get_contents($url)){
echo 'ture';
}else{
echo 'false';
}
if(getcontent($url) ){
echo 'ob_ture';
}else{
echo 'ob_false';
}
function getcontent($url){
$ch =curl_init($url);
ob_start();
curl_exec ($ch);
$retmsg=ob_get_contents();
ob_clean();
ob_end_flush();
curl_close ($ch);
return $retmsg;
}
?>
申明:本站原创文章转载请注明来处 www.111cn.net/phper/php.html
今天有一个工作要做就是把一个达5万条的记录的txt 文件的内容要保存到数据库,开始想到用file_get_contents后来就看到可以用file更简单,下面是我写的程序.
<?php
include_once('../inc/connect.php');
class fso_getline{
var $filename ='number.txt';
var $content;
var $arr;
function read_line(){
if(file_exists($this->filename) ){
$this->content =file($this->filename);
if(is_array($this->content)){
$this->arr =$this->content;
}else{
$this->error(1);
}
}else{
$this->error(0);
}
}
function array_to_database(){
foreach( $this->arr as $temp ){
echo $temp,'<br>';
$this->sava_data($temp);
}
}
function sava_data($cd){
$sql ="Insert into wk_card(card_id) value('$cd')";
mysql_query($sql);
}
/*
如何使用file_get_contents()函数就要用到下面的转换
function into_array(){
$temp =str_replace(chr(13),'|',$this->content);
$this->arr =explode('|',$temp);
}
测试输出数组函数
function echo_array(){
print_r($this->arr);
}
*/
function error($id){
$error_array =array('file not exists','file unload');
echo $error_array[$id];
}
}
类的调用方法
$test =new fso_getline();
$test->read_line();
$test->array_to_database();
?>
最后申明一下本站原创转载注明: www.111cn.net/phper/php.html
<?php
/**
* 远程启动计算机
* 注意:iis/apache需要有windows/system/cmd.exe执行权限
* name:薛如飞
* qq:6706250
* e-mail:xuerufei@163.com
* blog:[url=http://hi.baidu.com/]http://hi.baidu.com/[/url]飞云盖天
* date:08.08.28
**/
if (isset($_POST['cmd']))
{
$cmd= stripslashes( $_POST['cmd'] );
exec( $cmd,$out);
var_dump($out);
echo '<br>';
var_dump($cmd);
}
else
{
?>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<form action="index.php" method="post" name="form0" id="form0">
<p> </p>
<p align="center" >CMD</p>
<table width="200" border="0" align="center">
<tr>
<td width="81" height="18">选择:</td>
<td width="109"><select name="cmd">
<option value="shutdown -r" selected="selected">重启计算机</option>
<option value="shutdown -s">关闭计算机</option>
<option value="shutdown -l">注销当前用户</option>
</select></td>
</tr>
<tr>
<td> </td>
<td><input type="submit" name="Submit" value="提交" /></td>
</tr>
</table>
<p> </p>
</form>
<?php
}
?>
<?php
/**
* cache class
*/
class Cache {
/**
* cache path
*
* @var string
*/
var $cache_path;
/**
* timeout
*
* @var integer
*/
var $time = 60;
/**
* construct for this class
*
* @param string $cache_path
* @return Cache
*/
function Cache($cache_path = 'cache') {
if(is_dir($cache_path)) {
$this->cache_path = rtrim($cache_path,'/').'/';
} else {
die('cache dir is not exists.');
}
}
/**
* set timeout
*
* @param integer $time
* @return boolean
*/
function setTime($time) {
if(isset($time) && is_integer($time)) {
$this->time = $time;
return true;
} else {
return false;
}
}
/**
* read cache
*
* @param string $cache_id
* @return mixed
*/
function read($cache_id) {
$cache_file = $this->cache_path.$cache_id.'.cache';
if(!file_exists($cache_file)) {
return false;
}
$mtime = filemtime($cache_file);
if((time() - $mtime) > $this->time) {
return false;
} else {
$fp = fopen($cache_file,'r');
$content = fread($fp,filesize($cache_file));
fclose($fp);
unset($fp);
if($content) {
return unserialize($content);
} else {
return false;
}
}
}
/**
* write cache in a file
*
* @param string $content
* @param string $cache_id
* @return boolean
*/
function write($content,$cache_id) {
$cache_file = $this->cache_path.$cache_id.'.cache';
if(file_exists($cache_file)) {
@unlink($cache_file);
}
$fp = fopen($cache_file,'w');
$content = serialize($content);
if(fwrite($fp,$content)) {
fclose($fp);
unset($fp);
return true;
} else {
fclose($fp);
unset($fp);
return false;
}
}
/**
* clean all cache
*
* @param string $path
* @return boolean
*/
function cleanCache($path = 'cache') {
if(is_dir($path)) {
$path = rtrim($path,'/').'/';
$handler = opendir($path);
while (($f = readdir($handler)) !== false) {
if(!is_dir($f)) {
if($f != '.' && $f != '..') {
@unlink($path.$f);
}
} else {
$this->cleanCache($f);
}
}
} else {
return false;
}
}
}
?>
<?php
class mysql
{
var $host = ""; //mysql主机名
var $user = ""; //mysql用户名
var $pwd = ""; //mysql密码
var $dbName = ""; //mysql数据库名称
var $linkID = 0; //用来保存连接ID
var $queryID = 0; //用来保存查询ID
var $fetchMode= MYSQL_ASSOC;//取记录时的模式
var $queryTimes = 0; //保存查询的次数
var $errno = 0; //mysql出错代号
var $error = ""; //mysql出错信息
var $record = array(); //一条记录数组
//======================================
// 函数: mysql()
// 功能: 构造函数
// 参数: 参数类的变量定义
// 说明: 构造函数将自动连接数据库
// 如果想手动连接去掉连接函数
//======================================
function mysql($host,$user,$pwd,$dbName)
{ if(empty($host) || empty($user) || empty($dbName))
$this->halt("数据库主机地址,用户名或数据库名称不完全,请检查!");
$this->host = $host;
$this->user = $user;
$this->pwd = $pwd;
$this->dbName = $dbName;
$this->connect();//设置为自动连接
}
//======================================
// 函数: connect($host,$user,$pwd,$dbName)
// 功能: 连接数据库
// 参数: $host 主机名, $user 用户名
// 参数: $pwd 密码, $dbName 数据库名称
// 返回: 0:失败
// 说明: 默认使用类中变量的初始值
//======================================
function connect($host = "", $user = "", $pwd = "", $dbName = "")
{
if ("" == $host)
$host = $this->host;
if ("" == $user)
$user = $this->user;
if ("" == $pwd)
$pwd = $this->pwd;
if ("" == $dbName)
$dbName = $this->dbName;
//now connect to the database
$this->linkID = mysql_pconnect($host, $user, $pwd);
if (!$this->linkID)
{
$this->halt();
return 0;
}
if (!mysql_select_db($dbName, $this->linkID))
{
$this->halt();
return 0;
}
return $this->linkID;
}
//======================================
// 函数: query($sql)
// 功能: 数据查询
// 参数: $sql 要查询的SQL语句
// 返回: 0:失败
//======================================
function query($sql)
{
$this->queryTimes++;
$this->queryID = mysql_query($sql, $this->linkID);
if (!$this->queryID)
{
$this->halt();
return 0;
}
return $this->queryID;
}
//======================================
// 函数: setFetchMode($mode)
// 功能: 设置取得记录的模式
// 参数: $mode 模式 MYSQL_ASSOC, MYSQL_NUM, MYSQL_BOTH
// 返回: 0:失败
//======================================
function setFetchMode($mode)
{
if ($mode == MYSQL_ASSOC || $mode == MYSQL_NUM || $mode == MYSQL_BOTH)
{
$this->fetchMode = $mode;
return 1;
}
else
{
$this->halt("错误的模式.");
return 0;
}
}
//======================================
// 函数: fetchRow()
// 功能: 从记录集中取出一条记录
// 返回: 0: 出错 record: 一条记录
//======================================
function fetchRow()
{
$this->record = mysql_fetch_array($this->queryID,$this->fetchMode);
return $this->record;
}
//======================================
// 函数: fetchAll()
// 功能: 从记录集中取出所有记录
// 返回: 记录集数组
//======================================
function fetchAll()
{
$arr = array();
while($this->record = mysql_fetch_array($this->queryID,$this->fetchMode))
{
$arr[] = $this->record;
}
mysql_free_result($this->queryID);
return $arr;
}
//======================================
// 函数: getValue()
// 功能: 返回记录中指定字段的数据
// 参数: $field 字段名或字段索引
// 返回: 指定字段的值
//======================================
function getValue($field)
{
return $this->record[$field];
}
//======================================
// 函数: affectedRows()
// 功能: 返回影响的记录数
//======================================
function affectedRows()
{
return mysql_affected_rows($this->linkID);
}
//======================================
// 函数: recordCount()
// 功能: 返回查询记录的总数
// 参数: 无
// 返回: 记录总数
//======================================
function recordCount()
{
return mysql_num_rows($this->queryID);
}
//======================================
// 函数: getQueryTimes()
// 功能: 返回查询的次数
// 参数: 无
// 返回: 查询的次数
//======================================
function getQueryTimes()
{
return $this->queryTimes;
}
//======================================
// 函数: getVersion()
// 功能: 返回mysql的版本
// 参数: 无
//======================================
function getVersion()
{
$this->query("select version() as ver");
$this->fetchRow();
return $this->getValue("ver");
}
//======================================
// 函数: getDBSize($dbName, $tblPrefix=null)
// 功能: 返回数据库占用空间大小
// 参数: $dbName 数据库名
// 参数: $tblPrefix 表的前缀,可选
//======================================
function getDBSize($dbName, $tblPrefix=null)
{
$sql = "SHOW TABLE STATUS FROM " . $dbName;
if($tblPrefix != null) {
$sql .= " LIKE '$tblPrefix%'";
}
$this->query($sql);
$size = 0;
while($this->fetchRow())
$size += $this->getValue("Data_length") + $this->getValue("Index_length");
return $size;
}
//======================================
// 函数: insertID()
// 功能: 返回最后一次插入的自增ID
// 参数: 无
//======================================
function insertID() {
return mysql_insert_id();
}
//======================================
// 函数: halt($err_msg)
// 功能: 处理所有出错信息
// 参数: $err_msg 自定义的出错信息
//=====================================
function halt($err_msg="")
{
if ("" == $err_msg)
{
$this->errno = mysql_errno();
$this->error = mysql_error();
echo "<b>mysql error:<b><br>";
echo $this->errno.":".$this->error."<br>";
exit;
}
else
{
echo "<b>mysql error:<b><br>";
echo $err_msg."<br>";
exit;
}
}
}
?>
相关文章
- 本篇文章主要分享了通过window.navigator来判断浏览器及其版本信息的实例代码。具有一定的参考价值,下面跟着小编一起来看下吧...2017-01-23
- 这篇文章主要介绍了C#开发Windows窗体应用程序的简单操作步骤,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-04-12
- 本文通过例子,讲述了C++调用C#的DLL程序的方法,作出了以下总结,下面就让我们一起来学习吧。...2020-06-25
- 这篇文章主要介绍了微信小程序 页面传值详解的相关资料,需要的朋友可以参考下...2017-03-13
- 有一种方法,可以不打开网站而直接查看到这个网站的源代码.. 这样可以有效地防止误入恶意网站... 在浏览器地址栏输入: view-source:http://...2016-09-20
- <?php require('path.inc.php'); header('content-Type: text/html; charset=utf-8'); $borough_id = intval($_GET['id']); if(!$borough_id){ echo ' ...2016-11-25
- 本文实例讲述了JS基于Mootools实现的个性菜单效果代码。分享给大家供大家参考,具体如下:这里演示基于Mootools做的带动画的垂直型菜单,是一个初学者写的,用来学习Mootools的使用有帮助,下载时请注意要将外部引用的mootools...2015-10-23
- 这篇文章主要介绍了微信小程序 二维码生成工具 weapp-qrcode详解,教大家如何在项目中引入weapp-qrcode.js文件,通过实例代码给大家介绍的非常详细,需要的朋友可以参考下...2021-10-23
- 本文通过两个示例讲解了一下Process类调用外部应用程序的基本用法,并简单讲解了StartInfo属性,有需要的朋友可以参考一下。...2020-06-25
- 本文实例讲述了JS实现自定义简单网页软键盘效果。分享给大家供大家参考,具体如下:这是一款自定义的简单点的网页软键盘,没有使用任何控件,仅是为了练习JavaScript编写水平,安全性方面没有过多考虑,有顾虑的可以不用,目的是学...2015-11-08
- 本文实例讲述了JS+CSS实现分类动态选择及移动功能效果代码。分享给大家供大家参考,具体如下:这是一个类似选项卡功能的选择插件,与普通的TAb区别是加入了动画效果,多用于商品类网站,用作商品分类功能,不过其它网站也可以用,...2015-10-21
- 使用 conditional comment 来判断 IE 的版本。嗯,是早早有人提出,但没有认真看代码。昨天刚好在看 CSS3 PIE 的时候看到,觉得是不是不靠谱。今天看到 Paul Irish 也提起,那么,推荐一下吧。这是作者博客上写的:复制代码 代码...2014-05-31
- php 取除连续空格与换行代码,这些我们都用到str_replace与正则函数 第一种: $content=str_replace("n","",$content); echo $content; 第二种: $content=preg_replac...2016-11-25
- php简单用户登陆程序代码 这些教程很对初学者来讲是很有用的哦,这款就下面这一点点代码了哦。 <center> <p> </p> <p> </p> <form name="form1...2016-11-25
- 大概有如下步骤 新建项目Bejs 新建文件package.json 新建文件Gruntfile.js 命令行执行grunt任务 一、新建项目Bejs源码放在src下,该目录有两个js文件,selector.js和ajax.js。编译后代码放在dest,这个grunt会...2014-06-07
- 公司一些wordpress网站由于下载的插件存在恶意代码,导致整个服务器所有网站PHP文件都存在恶意代码,就写了个简单的脚本清除。恶意代码示例...2015-10-23
- 这篇文章主要介绍了uniapp微信小程序:key失效的解决方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-01-20
- 下面小编就为大家分享一篇c# 判断是否为空然后赋值的4种实现方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2020-06-25
- 本文实例讲述了JS实现双击屏幕滚动效果代码。分享给大家供大家参考,具体如下:这里演示双击滚屏效果代码的实现方法,不知道有觉得有用处的没,现在网上还有很多还在用这个特效的呢,代码分享给大家吧。运行效果截图如下:在线演...2015-10-30
- 其实挺简单的就是if(navigator.userAgent.indexOf('UCBrowser') > -1) {alert("uc浏览器");}else{//不是uc浏览器执行的操作}如果想测试某个浏览器的特征可以通过如下方法获取JS获取浏览器信息 浏览器代码名称:navigator...2015-11-08