+ -
当前位置:首页 → 问答吧 → 【飞翔de希望】5道题全部答案!

【飞翔de希望】5道题全部答案!

时间:2008-10-08

来源:互联网

由于论坛的加亮有些问题,看起来有些别看。
所以,建议大家如果想看的话,下载附件中的文件,执行run.php看结果。

另:第四题没有使用数据库,使用的文件读写,真正的程序应该使用数据库来读写。

[php]
<?php
/**
* @File name: function.inc.php
* @author: 飞翔de希望(Email:[email protected])
* @Date: Tue Oct 07 12:46:43 CST 2008
* @Copyright (C) , 2008, SUIKEE Co., Ltd.
* @Desription: 编程红宝书活动函数
*/
/**
* 1.遍历文件夹下的子文件夹及文件(需要PHP5以上版本)
*
* @param 目录 $dir
* @return  成功返回结果,失败返回false
*    结果结构: 不定维的多维数组。
*       第一维的键以数字索引顺排;
*       值如果是目录,包含title与child两个键,title对应的值是目录名称,child对应的是该目录下的所有目录或文件(与第一层目录结构相同,递归)
*       如果是文件,里面不包含 title与chile,值直接是文件名。
*/
function scanFile($dir)
{
if(!is_dir($dir)) //参数不是目录不操作
{
  return false;
}

$resultTemp = scandir($dir);

foreach ($resultTemp as $key=>$value)
{
  if($value == '.' || $value == '..') //不要.和..
  {
   unset($result[$key]);
   continue;
  }
  
  
  if(is_dir("{$dir}/{$value}")) //有下一层目录,继续扫
  {
   $result[$key]['title'] = $value;
   $result[$key]['child'] = scanFile("{$dir}/{$value}");
  }
  else //不是目录是文件
  {
   $result[$key] = $value;
  }
}

if(is_array($result)) //重新建立下标
{
  $result = array_values($result);
}

return $result;
}
/**
* 2.操作时间(加减时间)
*
* @param 日期(模式:yyyy-mm-dd) $date string
* @param 加减多少(加直接写数字,减使用负数) $do int
* @return String/Bool 结果
*/
function doTime($date, $do)
{
$date = explode('-', $date); //转换为数组
if(count($date) != 3) //格式不规范
{
  return false;
}

$date = mktime(0,0,0,$date[1], $date[2], $date[0]); //转换为时间戳
$date = $date + $do * 86400; //处理时间
return date('Y-m-d', $date);
}
/**
* 3.判断Email(采用文件形式读写数据,如果数据量较大[超过1000条],可采用数据库)
*
* @param Email $email
* @param 数据所在路径 $dataRoot
* @return 错误返回错误信息,正确返回true
*/
function checkEmail($email, $dataRoot)
{
$email = trim($email);
if(!$email)
{
  return '没有输入Email。';
}

//检查Email格式
if(!preg_match('/^([\w\d_]+)@([\w\d_]+)\.(\w{2,})$/', $email, $result))
{
  return 'Email格式错误。';
}

//不能使用sina、263、126、163、QQ的邮箱。
$noServer = array('sina', '263', '126', '163'); //禁止注册的服务器名
if(in_array($result[2], $noServer))
{
  return '该服务商禁止注册。';
}

//邮箱用户名不能全部为数字。
if(preg_match('/^\d+$/', $result[1]))
{
  return '邮箱用户名不能全部是数字。';
}

//邮箱服务器名不能为纯数字。
if(preg_match('/^\d+$/', $result[2]))
{
  return '邮箱服务器名不能全部是数字。';
}

//邮箱用户名不能和已注册过的邮箱用户名重复(一个邮箱不能注册多个用户)。
if (is_file($dataRoot))
{
  $data = file_get_contents($dataRoot);
  $data = explode(',', $data); //解析数据,变为数组
  if(in_array($email, $data))
  {
   return '该邮箱已存在。';
  }
}
return true;
}

?>
[/php]

4.SQL[code]SELECT * FROM `rank` a LEFT JOIN `user` b ON b.rankid = a.id LEFT JOIN `topic` c ON c.userid = b.id
WHERE a.edit = 1 AND c.belongid = 5;[/code]5.防注入
[php]
<?php
/**
* @File name: 5.php
* @author: 李枨煊
* @Date: Tue Oct 07 17:42:38 CST 2008
* @Copyright (C) , 2008, SUIKEE Co., Ltd.
* @Desription: 防止SQL注释的方法(不能执行)
*/

//5.1接收数据时,如果是数字,强制成INT型,如:
$page = isset($_GET['page']) ? (int)$_GET['page'] : 1;
//5.2接收字符串数据时,加入转义
$title = isset($_POST['title']) ? $_POST['title'] : '';
if(!get_magic_quotes_gpc())
{
$title = addslashes($title);
}
//5.3使用一些框架内置的查询方法可以自动转义,如ThinkPHP;
$user = D('user');
$result = $user->find(array('username'=>$username));
?>
[/php]

phpchina.zip (4.72 KB)

下载次数:9

2008-10-8 11:07

作者: 飞翔de希望   发布时间: 2008-10-08

run.php代码
[php]
<?php
/**
* @File name: run.php
* @author: 李枨煊
* @Date: Wed Oct 08 10:18:34 CST 2008
* @Copyright (C) , 2008, SUIKEE Co., Ltd.
* @Desription: 执行题
*/
header('Content-type: text/html; charset=utf-8');  //发送头信息(编码)
require('./function.inc.php');
//1.扫描无限层目录
echo '<h1>1.扫描无限层目录。</h1>';
$result = scanFile('./dir');
echo '<pre>';
print_r($result);
echo '</pre>';


//2.操作时间
echo '<h1>2.操作时间。</h1>';
$result = doTime('2008-1-1', -100);
echo "<div>2008-1-1的前100天是:{$result}</div>";
$result = doTime('2008-1-1', 200);
echo "<div>2008-1-1的后200天是:{$result}</div>";


//3.判断Email
echo '<h1 id="email">判断Email</h1>';
require('./3.html');
if (isset($_POST['email'])) //有提交注册Email
{
$email = $_POST['email'];
$dataRoot = './email.txt'; //Email数据路径

$result = checkEmail($email, $dataRoot); //检查Email
if($result === true) //检查成功!
{
  $fp = fopen($dataRoot, 'a+');
  fwrite($fp, $email.',');
  fclose($fp);
  
  echo "<div>恭喜您[{$email}]!<b>注册成功!</b></div>";
}
else //检查失败
{
  echo "<div>对不起[{$email}],操作失败。失败原因:<b>{$result}</b></div>";
}
}

//4.SQL查询
echo '<h1>4.SQL查询。</h1>';
require('./4_SQL.sql');
//5.防止SQL注入
echo '<h1>5.防止SQL注入</h1>';
highlight_file('./5.php');
?>
[/php]



1.执行结果[code]
Array
(
    [0] => Array
        (
            [title] => a
            [child] => Array
                (
                    [0] => Array
                        (
                            [title] => aa
                            [child] =>
                        )

                    [1] => Array
                        (
                            [title] => bb
                            [child] =>
                        )

                    [2] => Array
                        (
                            [title] => cc
                            [child] => Array
                                (
                                    [0] => 111.txt
                                    [1] => 123.txt
                                )

                        )

                )

        )

    [1] => Array
        (
            [title] => b
            [child] =>
        )

    [2] => Array
        (
            [title] => c
            [child] =>
        )

    [3] => Array
        (
            [title] => d
            [child] =>
        )

)[/code]2.执行结果。[code]
2008-1-1的前100天是:2007-09-23
2008-1-1的后200天是:2008-07-19

[/code]

作者: 飞翔de希望   发布时间: 2008-10-08

支持一下先!!!!!!!!!!

作者: liexusong   发布时间: 2008-10-08

注释十分清晰啊!

作者: PHPChina   发布时间: 2008-10-09

呵呵,在公司做东西就要这样,如果不这样,别人看起来很费劲的~~

作者: 飞翔de希望   发布时间: 2008-10-09

学习

作者: 倪昕   发布时间: 2008-10-12

方法都是最基本的模式。很细致

作者: 倪昕   发布时间: 2008-10-12

全写完了,人家咋写哈

作者: lxylxy888666   发布时间: 2008-10-12

LZ是我同学啊

作者: ilsanbao   发布时间: 2008-10-12

你是哪个噢?????????

作者: 飞翔de希望   发布时间: 2008-10-13

偶嘀怎么沉底了~~~顶起来咯~~~

作者: 飞翔de希望   发布时间: 2008-11-09

代码很简单嘛

作者: flyhacker   发布时间: 2008-11-25

顶。谢谢分享

作者: flyhacker   发布时间: 2008-11-25