php 后端开发学习

php 后端开发学习

hello.php:

 <?php
//设置cookies
setcookie("user", "runoob", time()+3600);
?>
<?php
session_start();
// 存储 session 数据
$_SESSION['views']=1;
?> <html>
<head>
<title>PHP 测试</title>
<meta charset="UTF-8">
</head>
<body style="text-align:center"> <!-- 输出--> <?php echo '<p>Hello World</p>' ; ?> <!-- for 循环-->
<?php
$x=array("one","two","three");
foreach ($x as $value)
{
echo $value . "<br>";
}
?> <!--if 判断-->
<?php
if (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== FALSE) {
?>
<h3>strpos() 肯定没有返回假 (FALSE)</h3>
<p>正在使用 Internet Explorer</p>
<?php
} else {
?>
<h3>strpos() 肯定返回假 (FALSE)</h3>
<center><b>没有使用 Internet Explorer</b></center>
<?php
}
?> <!--函数定义-->
<?php
function add($x,$y)
{
$total=$x+$y;
return $total;
} echo "1 + 16 = " . add(1,16);
?>
<br/> <!-- 面向对象-->
<?php
class Site {
/* 成员变量 */
var $url;
var $title; /* 成员函数 */
function setUrl($par){
$this->url = $par;
} function getUrl(){
echo $this->url . PHP_EOL;
} function setTitle($par){
$this->title = $par;
} function getTitle(){
echo $this->title . PHP_EOL;
}
} $runoob = new Site;
$taobao = new Site;
$google = new Site; // 调用成员函数,设置标题和URL
$runoob->setTitle( "菜鸟教程" );
$taobao->setTitle( "淘宝" );
$google->setTitle( "Google 搜索" ); $runoob->setUrl( 'www.runoob.com' );
$taobao->setUrl( 'www.taobao.com' );
$google->setUrl( 'www.google.com' ); // 调用成员函数,获取标题和URL
$runoob->getTitle();
$taobao->getTitle();
$google->getTitle(); $runoob->getUrl();
$taobao->getUrl();
$google->getUrl();
?> <!--form表单 , post 方法 -->
<form action="hello.php" method="post">
<p>姓名: <input type="text" name="name" /></p>
<p>年龄: <input type="text" name="age" /></p>
<p><input type="submit" /></p>
</form> 你好<?php echo htmlspecialchars($_POST['name']); ?>,
你 <?php echo (int)$_POST['age']; ?> 岁了。 <!-- form表单,get 方法-->
<form action="hello.php" method="get">
Name: <input type="text" name="name" /><br/>
Age: <input type="text" name="age" /><br/>
<input type="submit" />
</form> Welcome <?php echo $_GET["name"]; ?>.<br />
You are <?php echo $_GET["age"]; ?> years old! <br></br> <!--文件上传-->
<form action="upload_file.php" method="post"
enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file" />
<br />
<input type="submit" name="submit" value="Submit" />
</form> <!-- cookie -->
<?php
if (isset($_COOKIE["user"]))
echo "欢迎 " . $_COOKIE["user"] . "!<br>";
else
echo "普通访客!<br>";
?> <!--session-->
<?php echo "浏览量:". $_SESSION['views']; ?> <br/>
<!--创建数据库-->
<?php
$servername = "localhost";
$username = "root";
$password = "root"; // 创建连接
$conn = new mysqli($servername, $username, $password);
// 检测连接
if ($conn->connect_error) {
die("连接失败: " . $conn->connect_error);
} // 创建数据库
$sql = "CREATE DATABASE myDB";
if ($conn->query($sql) === TRUE) {
echo "数据库创建成功";
} else {
echo "Error creating database: " . $conn->error;
} $conn->close();
?>
<br/>
<!--创建数据表-->
<?php
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "myDB"; // 创建连接
$conn = new mysqli($servername, $username, $password, $dbname);
// 检测连接
if ($conn->connect_error) {
die("连接失败: " . $conn->connect_error);
} // 使用 sql 创建数据表
$sql = "CREATE TABLE MyGuests (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
firstname VARCHAR(30) NOT NULL,
lastname VARCHAR(30) NOT NULL,
email VARCHAR(50),
reg_date TIMESTAMP
)"; if ($conn->query($sql) === TRUE) {
echo "Table MyGuests created successfully";
} else {
echo "创建数据表错误: " . $conn->error;
} $conn->close();
?>
<br/>
<!--向数据库中添加新的记录-->
<?php
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "myDB"; // 创建连接
$conn = new mysqli($servername, $username, $password, $dbname);
// 检测连接
if ($conn->connect_error) {
die("连接失败: " . $conn->connect_error);
} $sql = "INSERT INTO MyGuests (firstname, lastname, email)
VALUES ('John', 'Doe', 'john@example.com')"; if ($conn->query($sql) === TRUE) {
echo "新记录插入成功";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
} $conn->close();
?>
</body>
</html>

uploada_file.php:

 <?php
// 允许上传的图片后缀
header("Content-type: text/html; charset=utf-8");
$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 "非法的文件格式";
}
?>

访问结果:

php 后端开发学习

上一篇:TCP-心跳


下一篇:前端HTML之页面结构