hello.php:
1 <?php 2 //设置cookies 3 setcookie("user", "runoob", time()+3600); 4 ?> 5 <?php 6 session_start(); 7 // 存储 session 数据 8 $_SESSION['views']=1; 9 ?> 10 11 <html> 12 <head> 13 <title>PHP 测试</title> 14 <meta charset="UTF-8"> 15 </head> 16 <body style="text-align:center"> 17 18 <!-- 输出--> 19 20 <?php echo '<p>Hello World</p>' ; ?> 21 22 23 <!-- for 循环--> 24 <?php 25 $x=array("one","two","three"); 26 foreach ($x as $value) 27 { 28 echo $value . "<br>"; 29 } 30 ?> 31 32 <!--if 判断--> 33 <?php 34 if (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== FALSE) { 35 ?> 36 <h3>strpos() 肯定没有返回假 (FALSE)</h3> 37 <p>正在使用 Internet Explorer</p> 38 <?php 39 } else { 40 ?> 41 <h3>strpos() 肯定返回假 (FALSE)</h3> 42 <center><b>没有使用 Internet Explorer</b></center> 43 <?php 44 } 45 ?> 46 47 <!--函数定义--> 48 <?php 49 function add($x,$y) 50 { 51 $total=$x+$y; 52 return $total; 53 } 54 55 echo "1 + 16 = " . add(1,16); 56 ?> 57 <br/> 58 59 60 61 <!-- 面向对象--> 62 <?php 63 class Site { 64 /* 成员变量 */ 65 var $url; 66 var $title; 67 68 /* 成员函数 */ 69 function setUrl($par){ 70 $this->url = $par; 71 } 72 73 function getUrl(){ 74 echo $this->url . PHP_EOL; 75 } 76 77 function setTitle($par){ 78 $this->title = $par; 79 } 80 81 function getTitle(){ 82 echo $this->title . PHP_EOL; 83 } 84 } 85 86 $runoob = new Site; 87 $taobao = new Site; 88 $google = new Site; 89 90 // 调用成员函数,设置标题和URL 91 $runoob->setTitle( "菜鸟教程" ); 92 $taobao->setTitle( "淘宝" ); 93 $google->setTitle( "Google 搜索" ); 94 95 $runoob->setUrl( 'www.runoob.com' ); 96 $taobao->setUrl( 'www.taobao.com' ); 97 $google->setUrl( 'www.google.com' ); 98 99 // 调用成员函数,获取标题和URL 100 $runoob->getTitle(); 101 $taobao->getTitle(); 102 $google->getTitle(); 103 104 $runoob->getUrl(); 105 $taobao->getUrl(); 106 $google->getUrl(); 107 ?> 108 109 110 <!--form表单 , post 方法 --> 111 <form action="hello.php" method="post"> 112 <p>姓名: <input type="text" name="name" /></p> 113 <p>年龄: <input type="text" name="age" /></p> 114 <p><input type="submit" /></p> 115 </form> 116 117 你好<?php echo htmlspecialchars($_POST['name']); ?>, 118 你 <?php echo (int)$_POST['age']; ?> 岁了。 119 120 121 <!-- form表单,get 方法--> 122 <form action="hello.php" method="get"> 123 Name: <input type="text" name="name" /><br/> 124 Age: <input type="text" name="age" /><br/> 125 <input type="submit" /> 126 </form> 127 128 Welcome <?php echo $_GET["name"]; ?>.<br /> 129 You are <?php echo $_GET["age"]; ?> years old! 130 131 <br></br> 132 133 134 <!--文件上传--> 135 <form action="upload_file.php" method="post" 136 enctype="multipart/form-data"> 137 <label for="file">Filename:</label> 138 <input type="file" name="file" id="file" /> 139 <br /> 140 <input type="submit" name="submit" value="Submit" /> 141 </form> 142 143 <!-- cookie --> 144 <?php 145 if (isset($_COOKIE["user"])) 146 echo "欢迎 " . $_COOKIE["user"] . "!<br>"; 147 else 148 echo "普通访客!<br>"; 149 ?> 150 151 152 <!--session--> 153 <?php echo "浏览量:". $_SESSION['views']; ?> 154 155 156 <br/> 157 <!--创建数据库--> 158 <?php 159 $servername = "localhost"; 160 $username = "root"; 161 $password = "root"; 162 163 // 创建连接 164 $conn = new mysqli($servername, $username, $password); 165 // 检测连接 166 if ($conn->connect_error) { 167 die("连接失败: " . $conn->connect_error); 168 } 169 170 // 创建数据库 171 $sql = "CREATE DATABASE myDB"; 172 if ($conn->query($sql) === TRUE) { 173 echo "数据库创建成功"; 174 } else { 175 echo "Error creating database: " . $conn->error; 176 } 177 178 $conn->close(); 179 ?> 180 <br/> 181 <!--创建数据表--> 182 <?php 183 $servername = "localhost"; 184 $username = "root"; 185 $password = "root"; 186 $dbname = "myDB"; 187 188 // 创建连接 189 $conn = new mysqli($servername, $username, $password, $dbname); 190 // 检测连接 191 if ($conn->connect_error) { 192 die("连接失败: " . $conn->connect_error); 193 } 194 195 // 使用 sql 创建数据表 196 $sql = "CREATE TABLE MyGuests ( 197 id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY, 198 firstname VARCHAR(30) NOT NULL, 199 lastname VARCHAR(30) NOT NULL, 200 email VARCHAR(50), 201 reg_date TIMESTAMP 202 )"; 203 204 if ($conn->query($sql) === TRUE) { 205 echo "Table MyGuests created successfully"; 206 } else { 207 echo "创建数据表错误: " . $conn->error; 208 } 209 210 $conn->close(); 211 ?> 212 <br/> 213 <!--向数据库中添加新的记录--> 214 <?php 215 $servername = "localhost"; 216 $username = "root"; 217 $password = "root"; 218 $dbname = "myDB"; 219 220 // 创建连接 221 $conn = new mysqli($servername, $username, $password, $dbname); 222 // 检测连接 223 if ($conn->connect_error) { 224 die("连接失败: " . $conn->connect_error); 225 } 226 227 $sql = "INSERT INTO MyGuests (firstname, lastname, email) 228 VALUES ('John', 'Doe', 'john@example.com')"; 229 230 if ($conn->query($sql) === TRUE) { 231 echo "新记录插入成功"; 232 } else { 233 echo "Error: " . $sql . "<br>" . $conn->error; 234 } 235 236 $conn->close(); 237 ?> 238 </body> 239 </html>
uploada_file.php:
1 <?php 2 // 允许上传的图片后缀 3 header("Content-type: text/html; charset=utf-8"); 4 $allowedExts = array("gif", "jpeg", "jpg", "png"); 5 $temp = explode(".", $_FILES["file"]["name"]); 6 echo $_FILES["file"]["size"]; 7 $extension = end($temp); // 获取文件后缀名 8 if ((($_FILES["file"]["type"] == "image/gif") 9 || ($_FILES["file"]["type"] == "image/jpeg") 10 || ($_FILES["file"]["type"] == "image/jpg") 11 || ($_FILES["file"]["type"] == "image/pjpeg") 12 || ($_FILES["file"]["type"] == "image/x-png") 13 || ($_FILES["file"]["type"] == "image/png")) 14 && ($_FILES["file"]["size"] < 204800) // 小于 200 kb 15 && in_array($extension, $allowedExts)) 16 { 17 if ($_FILES["file"]["error"] > 0) 18 { 19 echo "错误:: " . $_FILES["file"]["error"] . "<br>"; 20 } 21 else 22 { 23 echo "上传文件名: " . $_FILES["file"]["name"] . "<br>"; 24 echo "文件类型: " . $_FILES["file"]["type"] . "<br>"; 25 echo "文件大小: " . ($_FILES["file"]["size"] / 1024) . " kB<br>"; 26 echo "文件临时存储的位置: " . $_FILES["file"]["tmp_name"] . "<br>"; 27 28 // 判断当期目录下的 upload 目录是否存在该文件 29 // 如果没有 upload 目录,你需要创建它,upload 目录权限为 777 30 if (file_exists("upload/" . $_FILES["file"]["name"])) 31 { 32 echo $_FILES["file"]["name"] . " 文件已经存在。 "; 33 } 34 else 35 { 36 // 如果 upload 目录不存在该文件则将文件上传到 upload 目录下 37 move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $_FILES["file"]["name"]); 38 echo "文件存储在: " . "upload/" . $_FILES["file"]["name"]; 39 } 40 } 41 } 42 else 43 { 44 echo "非法的文件格式"; 45 } 46 ?>
访问结果: