- 判断远程URL是否有效
- /*=========================================================================================================================*/
-
前提
需要判断远程URL是否有效,远程url包括远程图片,网页,视频等等元素
解决办法:
使用PHP解决
- 使用file_get_contents函数,不过优缺点如果url无法访问,会出现终止程序问题
- 使用curl返回,然后判断是否正确执行
- 使用get_headers函数,根据HTTP返回值查看是否有200
使用js解决:
- 使用原生的js函数ActiveXObject,仅支持ie内核的浏览器
- 使用jq扩展
本文主要介绍PHP解决办法中的第三种,这个方法很少用到,但是感觉又起来还不错,
get_headers需要支持
- php_openssl支持 查看phpinfo看看是否开启
- allow_url_fopen=on 修改php.ini,运行使用远程打开
函数介绍:
array get_headers ( string $url [, int $format ] )
get_headers() 返回一个数组,包含有服务器响应一个 HTTP 请求所发送的标头。如果失败则返回 FALSE 并发出一条 E_WARNING 级别的错误信息。
如果将可选的 format 参数设为 1,则 get_headers() 会解析相应的信息并设定数组的键名。 例如:
简单的例子:
<?php
$url = "http://cn.php.net/images/php.gif";
$array = get_headers($url,1);
if(preg_match('/200/',$array[0])){
echo "<pre/>";
print_r($array);
}else{
echo "无效url资源!";
}解释:判断远程图片ur是否有效,根据返回值HTTP中是否有200信息,判断是否是有效url资源
测试结果:
可以看到正确返回 -----------------------测试ok
优点缺点:
需要allow_url_fopen=on 开启,有点和file_get_contents函数使用条件类似,但是返回值比较少,可以使用
function_exists判断该方法是否可以使用
使用cURL:
- function check_remote_file_exists($url)
- {
- $curl = curl_init($url);
- // 不取回数据
- curl_setopt($curl, CURLOPT_NOBODY, true);
- // 发送请求
- $result = curl_exec($curl);
- $found = false;
- // 如果请求没有发送失败
- if ($result !== false) {
- // 再检查http响应码是否为200
- $statusCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
- if ($statusCode == 200) {
- $found = true;
- }
- }
- curl_close($curl);
- return $found;
- }
其他实现远程判断url是否有效的方法:
解决方案一: XMLHTTP方案
<script language= "javascript"> function getURL(url) { var xmlhttp = new ActiveXObject( "Microsoft.XMLHTTP"); xmlhttp.open("GET", url, false); xmlhttp.send(); if(xmlhttp.readyState==4) { if(xmlhttp.Status != 200) alert("不存在"); return xmlhttp.Status==200; } return false; } </script> <a href= "http://www.csdn.net/aaa.asp " onclick= "return getURL(this.href) "> csdn </a>
缺点: 使用ActiveXObject, 所以是IE Only. 非IE内核浏览器不可用.
解决方案二: jQuery扩展
如果针对一个具体的URL,光用jQuery,不需要插件可以这样:
- $.ajax({
- url: 'http://some.url.com',
- type: 'GET',
- complete: function(response) {
- if(response.status == 200) {
- alert('有效');
- } else {
- alert('无效');
- }
- }
- });
- /*=========================================================================================================================*/
-
- 防止SQL注入的几个过滤函数
- /*==============================================================================================*/
- /**
- * discuz!防注入的函数
- */
- $magic_quotes_gpc = get_magic_quotes_gpc();
- @extract(daddslashes($_COOKIE));
- @extract(daddslashes($_POST));
- @extract(daddslashes($_GET));
- if(!$magic_quotes_gpc) {
- $_FILES = daddslashes($_FILES);
- }
- function daddslashes($string, $force = 0) {
- if(!$GLOBALS['magic_quotes_gpc'] || $force) {
- if(is_array($string)) {
- foreach($string as $key => $val) {
- $string[$key] = daddslashes($val, $force);
- }
- } else {
- $string = addslashes($string);
- }
- }
- return $string;
- }
- /*==============================================================================================*/
- /**
- * 函数名称:inject_check()
- * 函数作用:检测提交的值是不是含有SQL注射的字符,防止注射,保护服务器安全
- * 参 数:$sql_str: 提交的变量
- * 返 回 值:返回检测结果,ture or false
- */
- function inject_check($sql_str) {
- return eregi('select|insert|and|or|update|delete|\'|\/\*|\*|\.\.\/|\.\/|union|into|load_file|outfile', $sql_str); // 进行过滤
- }
- /**
- * 函数名称:verify_id()
- * 函数作用:校验提交的ID类值是否合法
- * 参 数:$id: 提交的ID值
- * 返 回 值:返回处理后的ID
- */
- function verify_id($id=null) {
- if(!$id) { // 是否为空判断
- exit('没有提交参数!');
- } elseif(inject_check($id)) { // 注射判断
- exit('提交的参数非法!');
- } elseif(!is_numeric($id)) { // 数字判断
- exit('提交的参数非法!');
- }
- $id = intval($id); // 整型化
- return $id;
- }
- /**
- * 函数名称:str_check()
- * 函数作用:对提交的字符串进行过滤
- * 参 数:$var: 要处理的字符串
- * 返 回 值:返回过滤后的字符串
- */
- function str_check( $str ) {
- if(!get_magic_quotes_gpc()) { // 判断magic_quotes_gpc是否打开
- $str = addslashes($str); // 进行过滤
- }
- $str = str_replace("_", "\_", $str); // 把 '_'过滤掉
- $str = str_replace("%", "\%", $str); // 把 '%'过滤掉
- return $str;
- }
- /**
- * 函数名称:post_check()
- * 函数作用:对提交的编辑内容进行处理
- * 参 数:$post: 要提交的内容
- * 返 回 值:$post: 返回过滤后的内容
- */
- function post_check($post) {
- if(!get_magic_quotes_gpc()) { // 判断magic_quotes_gpc是否为打开
- $post = addslashes($post); // 进行magic_quotes_gpc没有打开的情况对提交数据的过滤
- }
- $post = str_replace("_", "\_", $post); // 把 '_'过滤掉
- $post = str_replace("%", "\%", $post); // 把 '%'过滤掉
- $post = nl2br($post); // 回车转换
- $post = htmlspecialchars($post); // html标记转换
- return $post;
- }
-
PHP一些有用的函数
最新推荐文章于 2021-03-20 12:38:11 发布