• 吴裕雄--天生自然 PHP开发学习:高级


    <?php
    echo date("Y/m/d") . "<br>";
    echo date("Y.m.d") . "<br>";
    echo date("Y-m-d");
    ?>

    <html>
    <head>
    <meta charset="utf-8">
    <title>菜鸟教程(runoob.com)</title>
    </head>
    <body>
    
    <?php include 'header.php'; ?>
    <h1>欢迎来到我的主页!</h1>
    <p>一些文本。</p>
    
    </body>
    </html>
    <html>
    <head>
    <meta charset="utf-8">
    <title>菜鸟教程(runoob.com)</title>
    </head>
    <body>
    
    <div class="leftmenu">
    <?php include 'menu.php'; ?>
    </div>
    <h1>欢迎来到我的主页!</h1>
    <p>一些文本。</p>
    
    </body>
    </html>
    <?php
    $color='red';
    $car='BMW';
    ?>
    <html>
    <head>
    <meta charset="utf-8">
    <title>菜鸟教程(runoob.com)</title>
    </head>
    <body>
    
    <h1>欢迎来到我的主页!</h1>
    <?php 
    include 'vars.php';
    echo "I have a $color $car"; // I have a red BMW
    ?>
    
    </body>
    </html>
    <html>
    <body>
    
    <?php
    $file=fopen("welcome.txt","r");
    ?>
    
    </body>
    </html>
    <html>
    <body>
    
    <?php
    $file=fopen("welcome.txt","r") or exit("Unable to open file!");
    ?>
    
    </body>
    </html>
    <?php
    $file = fopen("test.txt","r");
    
    //执行一些代码
    
    fclose($file);
    ?>
    <?php
    $file = fopen("welcome.txt", "r") or exit("无法打开文件!");
    // 读取文件每一行,直到文件结尾
    while(!feof($file))
    {
        echo fgets($file). "<br>";
    }
    fclose($file);
    ?>
    <?php
    $file=fopen("welcome.txt","r") or exit("无法打开文件!");
    while (!feof($file))
    {
        echo fgetc($file);
    }
    fclose($file);
    ?>
    <?php
    // 允许上传的图片后缀
    $allowedExts = array("gif", "jpeg", "jpg", "png");
    $temp = explode(".", $_FILES["file"]["name"]);
    echo $_FILES["file"]["size"];
    $extension = end($temp);     // 获取文件后缀名
    if ((($_FILES["file"]["type"] == "image/gif")
        || ($_FILES["file"]["type"] == "image/jpeg")
        || ($_FILES["file"]["type"] == "image/jpg")
        || ($_FILES["file"]["type"] == "image/pjpeg")
        || ($_FILES["file"]["type"] == "image/x-png")
        || ($_FILES["file"]["type"] == "image/png"))
        && ($_FILES["file"]["size"] < 204800)   // 小于 200 kb
        && in_array($extension, $allowedExts))
    {
        if ($_FILES["file"]["error"] > 0)
        {
            echo "错误:: " . $_FILES["file"]["error"] . "<br>";
        }
        else
        {
            echo "上传文件名: " . $_FILES["file"]["name"] . "<br>";
            echo "文件类型: " . $_FILES["file"]["type"] . "<br>";
            echo "文件大小: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
            echo "文件临时存储的位置: " . $_FILES["file"]["tmp_name"] . "<br>";
            
            // 判断当期目录下的 upload 目录是否存在该文件
            // 如果没有 upload 目录,你需要创建它,upload 目录权限为 777
            if (file_exists("upload/" . $_FILES["file"]["name"]))
            {
                echo $_FILES["file"]["name"] . " 文件已经存在。 ";
            }
            else
            {
                // 如果 upload 目录不存在该文件则将文件上传到 upload 目录下
                move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $_FILES["file"]["name"]);
                echo "文件存储在: " . "upload/" . $_FILES["file"]["name"];
            }
        }
    }
    else
    {
        echo "非法的文件格式";
    }
    ?>
    <html>
    <head>
    <meta charset="utf-8">
    <title>菜鸟教程(runoob.com)</title>
    </head>
    <body>
    
    <form action="upload_file.php" method="post" enctype="multipart/form-data">
        <label for="file">文件名:</label>
        <input type="file" name="file" id="file"><br>
        <input type="submit" name="submit" value="提交">
    </form>
    
    </body>
    </html>
    <?php
    if ($_FILES["file"]["error"] > 0)
    {
        echo "错误:" . $_FILES["file"]["error"] . "<br>";
    }
    else
    {
        echo "上传文件名: " . $_FILES["file"]["name"] . "<br>";
        echo "文件类型: " . $_FILES["file"]["type"] . "<br>";
        echo "文件大小: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
        echo "文件临时存储的位置: " . $_FILES["file"]["tmp_name"];
    }
    ?>
    <?php
    // 允许上传的图片后缀
    $allowedExts = array("gif", "jpeg", "jpg", "png");
    $temp = explode(".", $_FILES["file"]["name"]);
    $extension = end($temp);        // 获取文件后缀名
    if ((($_FILES["file"]["type"] == "image/gif")
    || ($_FILES["file"]["type"] == "image/jpeg")
    || ($_FILES["file"]["type"] == "image/jpg")
    || ($_FILES["file"]["type"] == "image/pjpeg")
    || ($_FILES["file"]["type"] == "image/x-png")
    || ($_FILES["file"]["type"] == "image/png"))
    && ($_FILES["file"]["size"] < 204800)    // 小于 200 kb
    && in_array($extension, $allowedExts))
    {
        if ($_FILES["file"]["error"] > 0)
        {
            echo "错误:: " . $_FILES["file"]["error"] . "<br>";
        }
        else
        {
            echo "上传文件名: " . $_FILES["file"]["name"] . "<br>";
            echo "文件类型: " . $_FILES["file"]["type"] . "<br>";
            echo "文件大小: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
            echo "文件临时存储的位置: " . $_FILES["file"]["tmp_name"];
        }
    }
    else
    {
        echo "非法的文件格式";
    }
    ?>
    <?php
    // 输出 cookie 值
    echo $_COOKIE["user"];
    
    // 查看所有 cookie
    print_r($_COOKIE);
    ?>
    <html>
    <head>
    <meta charset="utf-8">
    <title>菜鸟教程(runoob.com)</title>
    </head>
    <body>
    
    <?php
    if (isset($_COOKIE["user"]))
        echo "欢迎 " . $_COOKIE["user"] . "!<br>";
    else
        echo "普通访客!<br>";
    ?>
    
    </body>
    </html>
    <?php
    // 设置 cookie 过期时间为过去 1 小时
    setcookie("user", "", time()-3600);
    ?>
    <html>
    <head>
    <meta charset="utf-8">
    <title>菜鸟教程(runoob.com)</title>
    </head>
    <body>
    
    <form action="welcome.php" method="post">
    名字: <input type="text" name="name">
    年龄: <input type="text" name="age">
    <input type="submit">
    </form>
    
    </body>
    </html>
    <html>
    <head>
    <meta charset="utf-8">
    <title>菜鸟教程(runoob.com)</title>
    </head>
    <body>
    
    欢迎 <?php echo $_POST["name"]; ?>.<br><?php echo $_POST["age"]; ?> 岁了。
    
    </body>
    </html>
    <?php
    session_start();
    // 存储 session 数据
    $_SESSION['views']=1;
    ?>
     
    <html>
    <head>
    <meta charset="utf-8">
    <title>菜鸟教程(runoob.com)</title>
    </head>
    <body>
     
    <?php
    // 检索 session 数据
    echo "浏览量:". $_SESSION['views'];
    ?>
     
    </body>
    </html>
    <?php
    session_start();
     
    if(isset($_SESSION['views']))
    {
        $_SESSION['views']=$_SESSION['views']+1;
    }
    else
    {
        $_SESSION['views']=1;
    }
    echo "浏览量:". $_SESSION['views'];
    ?>
    <?php
    session_start();
    if(isset($_SESSION['views']))
    {
        unset($_SESSION['views']);
    }
    ?>
    <?php
    session_destroy();
    ?>
    <?php
    $to = "someone@example.com";         // 邮件接收者
    $subject = "参数邮件";                // 邮件标题
    $message = "Hello! 这是邮件的内容。";  // 邮件正文
    $from = "someonelse@example.com";   // 邮件发送者
    $headers = "From:" . $from;         // 头部信息设置
    mail($to,$subject,$message,$headers);
    echo "邮件已发送";
    ?>
    <html>
    <head>
    <meta charset="utf-8">
    <title>菜鸟教程(runoob.com)</title>
    </head>
    <body>
    
    <?php
    if (isset($_REQUEST['email'])) { // 如果接收到邮箱参数则发送邮件
        // 发送邮件
        $email = $_REQUEST['email'] ;
        $subject = $_REQUEST['subject'] ;
        $message = $_REQUEST['message'] ;
        mail("someone@example.com", $subject,
        $message, "From:" . $email);
        echo "邮件发送成功";
    } else { // 如果没有邮箱参数则显示表单
        echo "<form method='post' action='mailform.php'>
        Email: <input name='email' type='text'><br>
        Subject: <input name='subject' type='text'><br>
        Message:<br>
        <textarea name='message' rows='15' cols='40'>
        </textarea><br>
        <input type='submit'>
        </form>";
    }
    ?>
    
    </body>
    </html>
    <html>
    <head>
    <meta charset="utf-8">
    <title>菜鸟教程(runoob.com)</title>
    </head>
    <body>
    <?php
    function spamcheck($field)
    {
        // filter_var() 过滤 e-mail
        // 使用 FILTER_SANITIZE_EMAIL
        $field=filter_var($field, FILTER_SANITIZE_EMAIL);
    
        //filter_var() 过滤 e-mail
        // 使用 FILTER_VALIDATE_EMAIL
        if(filter_var($field, FILTER_VALIDATE_EMAIL))
        {
            return TRUE;
        }
        else
        {
            return FALSE;
        }
    }
    
    if (isset($_REQUEST['email']))
    {
        // 如果接收到邮箱参数则发送邮件
    
        // 判断邮箱是否合法
        $mailcheck = spamcheck($_REQUEST['email']);
        if ($mailcheck==FALSE)
        {
            echo "非法输入";
        }
        else
        {    
            // 发送邮件
            $email = $_REQUEST['email'] ;
            $subject = $_REQUEST['subject'] ;
            $message = $_REQUEST['message'] ;
            mail("someone@example.com", "Subject: $subject",
            $message, "From: $email" );
            echo "Thank you for using our mail form";
        }
    }
    else
    { 
        // 如果没有邮箱参数则显示表单
        echo "<form method='post' action='mailform.php'>
        Email: <input name='email' type='text'><br>
        Subject: <input name='subject' type='text'><br>
        Message:<br>
        <textarea name='message' rows='15' cols='40'>
        </textarea><br>
        <input type='submit'>
        </form>";
    }
    ?>
    
    </body>
    </html>
    <?php
    // 错误处理函数
    function customError($errno, $errstr)
    {
        echo "<b>Error:</b> [$errno] $errstr<br>";
        echo "脚本结束";
        die();
    }
    
    // 设置错误处理函数
    set_error_handler("customError",E_USER_WARNING);
    
    // 触发错误
    $test=2;
    if ($test>1)
    {
        trigger_error("变量值必须小于等于 1",E_USER_WARNING);
    }
    ?>

    <?php
    // 错误处理函数
    function customError($errno, $errstr)
    {
        echo "<b>Error:</b> [$errno] $errstr<br>";
        echo "已通知网站管理员";
        error_log("Error: [$errno] $errstr",1,
        "someone@example.com","From: webmaster@example.com");
    }
    
    // 设置错误处理函数
    set_error_handler("customError",E_USER_WARNING);
    
    // 触发错误
    $test=2;
    if ($test>1)
    {
        trigger_error("变量值必须小于等于 1",E_USER_WARNING);
    }
    ?>

    <?php
    // 创建一个有异常处理的函数
    function checkNum($number)
    {
        if($number>1)
        {
            throw new Exception("变量值必须小于等于 1");
        }
            return true;
    }
        
    // 在 try 块 触发异常
    try
    {
        checkNum(2);
        // 如果抛出异常,以下文本不会输出
        echo '如果输出该内容,说明 $number 变量';
    }
    // 捕获异常
    catch(Exception $e)
    {
        echo 'Message: ' .$e->getMessage();
    }
    ?>

    <?php
    class customException extends Exception
    {
        public function errorMessage()
        {
            // 错误信息
            $errorMsg = '错误行号 '.$this->getLine().' in '.$this->getFile()
            .': <b>'.$this->getMessage().'</b> 不是一个合法的 E-Mail 地址';
            return $errorMsg;
        }
    }
     
    $email = "someone@example...com";
     
    try
    {
        // 检测邮箱
        if(filter_var($email, FILTER_VALIDATE_EMAIL) === FALSE)
        {
            // 如果是个不合法的邮箱地址,抛出异常
            throw new customException($email);
        }
    }
     
    catch (customException $e)
    {
    //display custom message
    echo $e->errorMessage();
    }
    ?>
    <?php
    $int = 123;
     
    if(!filter_var($int, FILTER_VALIDATE_INT))
    {
        echo("不是一个合法的整数");
    }
    else
    {
        echo("是个合法的整数");
    }
    ?>
    <?php
    $var=300;
     
    $int_options = array(
        "options"=>array
        (
            "min_range"=>0,
            "max_range"=>256
        )
    );
     
    if(!filter_var($var, FILTER_VALIDATE_INT, $int_options))
    {
        echo("不是一个合法的整数");
    }
    else
    {
        echo("是个合法的整数");
    }
    ?>
    <?php
    if(!filter_has_var(INPUT_GET, "email"))
    {
        echo("没有 email 参数");
    }
    else
    {
        if (!filter_input(INPUT_GET, "email", FILTER_VALIDATE_EMAIL))
        {
            echo "不是一个合法的 E-Mail";
        }
        else
        {
            echo "是一个合法的 E-Mail";
        }
    }
    ?>
    <?php
    $filters = array
    (
        "name" => array
        (
            "filter"=>FILTER_SANITIZE_STRING
        ),
        "age" => array
        (
            "filter"=>FILTER_VALIDATE_INT,
            "options"=>array
            (
                "min_range"=>1,
                "max_range"=>120
            )
        ),
        "email"=> FILTER_VALIDATE_EMAIL
    );
     
    $result = filter_input_array(INPUT_GET, $filters);
     
    if (!$result["age"])
    {
        echo("年龄必须在 1 到 120 之间。<br>");
    }
    elseif(!$result["email"])
    {
        echo("E-Mail 不合法<br>");
    }
    else
    {
        echo("输入正确");
    }
    ?>
    <?php
    function convertSpace($string)
    {
        return str_replace("_", ".", $string);
    }
     
    $string = "www_runoob_com!";
     
    echo filter_var($string, FILTER_CALLBACK,
    array("options"=>"convertSpace"));
    ?>
    <?php
    $int = 122;
    $min = 1;
    $max = 200;
    
    if (filter_var($int, FILTER_VALIDATE_INT, array("options" => array("min_range"=>$min, "max_range"=>$max))) === false) {
        echo("变量值不在合法范围内");
    } else {
        echo("变量值在合法范围内");
    }
    ?>
    <?php
    $ip = "2001:0db8:85a3:08d3:1319:8a2e:0370:7334";
    
    if (!filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) === false) {
        echo("$ip 是一个 IPv6 地址");
    } else {
        echo("$ip 不是一个 IPv6 地址");
    }
    ?>
    <?php
    $url = "http://www.runoob.com";
    
    if (!filter_var($url, FILTER_VALIDATE_URL, FILTER_FLAG_QUERY_REQUIRED) === false) {
        echo("$url 是一个合法的 URL");
    } else {
        echo("$url 不是一个合法的 URL");
    }
    ?>
    <?php
    $str = "<h1>Hello WorldÆØÅ!</h1>";
    
    $newstr = filter_var($str, FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_HIGH);
    echo $newstr;
    ?>
    <?php
       $arr = array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5);
       echo json_encode($arr);
    ?>
    <?php
       class Emp {
           public $name = "";
           public $hobbies  = "";
           public $birthdate = "";
       }
       $e = new Emp();
       $e->name = "sachin";
       $e->hobbies  = "sports";
       $e->birthdate = date('m/d/Y h:i:s a', "8/5/1974 12:20:03 p");
       $e->birthdate = date('m/d/Y h:i:s a', strtotime("8/5/1974 12:20:03"));
    
       echo json_encode($e);
    ?>
    <?php
       $json = '{"a":1,"b":2,"c":3,"d":4,"e":5}';
    
       var_dump(json_decode($json));
       var_dump(json_decode($json, true));
    ?>

  • 相关阅读:
    Android NDK 学习之传递类对象
    https://www.aminer.cn/ AI研究
    MYSQL 的一些文件及说明
    Windows下移动MariaDB数据目录 (转!)
    MariaDB Galera Cluster 10.1 只支持 LINUX ?!
    MARIADB 在 OPENSUSE 的安装。
    “命令行程序”的通用图形界面 (转)
    代码生成的地址:mygeneration
    上海力软--快速开发框架
    康力优蓝机器人 -- 优友U05类人型机器人发布
  • 原文地址:https://www.cnblogs.com/tszr/p/10948412.html
Copyright © 2020-2023  润新知