_ G E T , \_GET, _GET,_POST
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form action="demo.php" method="GET">
用户:<input type="text" name="name"><br>
密码:<input type="password" name="pwd"><br>
<input type="submit" name="submit" value="登录">
</form>
</body>
</html>
请求的页面是demo.php
可以看到URL发生了变化
请求的参数是?
后面的数据(GET:将参数拼接到URL中),多个参数之间以&
分割。
http://localhost:63342/course/demo.php?name=kali&pwd=123&submit=%E7%99%BB%E5%BD%95
<?php
// echo 输出到前端浏览器,需要设置编码
echo "<meta charset='utf-8'>";
header("Content-Type:text/html;charset=utf-8");
// name=kali&pwd=123&submit=登录
$name = $_GET['name'];
$password = $_GET['pwd'];
echo "当前用户的账号是".$name.",密码是".$password;
?>
也可以在PHP页面中写HTML
<?php
// echo 输出到前端浏览器,需要设置编码
// echo "<meta charset='utf-8'>";
header("Content-Type:text/html;charset=utf-8");
echo '<!--#代表提交到当前数据-->
<form action="#" method="GET">
用户:<input type="text" name="name"><br>
密码:<input type="password" name="pwd"><br>
<input type="submit" name="submit" value="登录">
</form>';
// name=kali&pwd=123&submit=登录
// name=&pwd=&submit=登录#
// @忽略报错
$name = @$_GET['name'];
$password = @$_GET['pwd'];
// isset用于检测变量是否设置,不为NULL,返回值是bool。注意,""不是NULL,所以isset("")的返回值是TRUE。
if ( (isset($name)&&$name) && (isset($password)&&$password) ) {
/*判断当前提交的数据是否为空,不为空的情况下执行下面的代码*/
echo "当前用户的账号是".$name.",密码是".$password;
}
?>
LAMP,php.ini文件的位置
/ect/php.ini
/etc/php/7.3/apache2/php.ini(ubuntu)
判断数据类型var_dump()
字符串常用方法:
-
.
拼接字符串 -
strlen()
计算字符串的长度
<?php
$string = "123456";
echo strlen($string);
echo $string[0];
strpos
(字符串,需要寻找的字符串)查找字符串,返回首次匹配的索引。
<?php
$string = "123456";
$str = strrev($string);
echo "\n";
echo $string;
echo $str;
// strrev方法的返回值是一个逆序的字符串,但是原字符串并没有被修改
substr
<?php
$string = "kali linux";
// $str2 = substr($string, 5);
$str3 = substr($string, -1);
// echo $str2;
echo $str3;
trim
去除字符串开头和结尾处的空格
<?php
$string = " Kali Linux ";
echo $string;
intdiv
整除
<?php
echo intdiv(100, 3);
在命令行下用解释器运行PHP脚本
进入到解释器php.exe所在的目录C:\phpstudy_pro\Extensions\php\php7.3.4nts>
,
输入命令php.exe -h
,得到帮助信息:
-r <code> Run PHP <code> without using script tags <?..?>
-f <file> Parse and execute <file>.
-r
参数,运行PHP代码不需要使用脚本标签<? ?>
C:\phpstudy_pro\Extensions\php\php7.3.4nts>php.exe -r "echo(intdiv(100, 3));" 33
-f
参数,解析并执行指定的文件
C:\phpstudy_pro\Extensions\php\php7.3.4nts>php.exe -f C:\phpstudy_pro\WWW\php\course\strlen.php 33
关联数组和数字型数组
<?php
$arrayName = array('name'=>"phos", "age"=>21, "gender"=>false); // 关联数组
$array = array('1',"zhangsa", 20, true, "crimnal" ); //数字型数组
echo $arrayName["name"];
echo $array[4];
<?php
$arrayName = array('name'=>"phos", "age"=>21, "gender"=>true); // 关联数组
$array = array('1',"zhangsa", 20, true, "crimnal" ); //数字型数组
foreach ($arrayName as $key => $value) {
// $key 存储的是键,$value存储的是值
echo $key."\n";
echo $value."\n";
}
foreach ($array as $value) {
// $value存储的是值
echo $value.PHP_EOL;
}
数组的排序:sort()
升序,rsort()
降序
超级全局变量:
$GLOBALS
包含
<?php
$string = "123"; // 变量
define("DEMO", true); // 定义常量
print_r($GLOBALS); // 超级全局变量
demo.php
文件
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form action="#" method="GET">
usrname: <input type="text" name="name">
password: <input type="password" name="pwd">
<input type="submit" name="submit" value="login">
</form>
</body>
</html>
<?php
print_r($GLOBALS)
?>
在表单中输入Misaka, Mikoto,点击login,输出为:
Array ( [_GET] => Array ( [name] => Misaka [pwd] => Mikoto [submit] => login ) )
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form action="#" method="POST">
usrname: <input type="text" name="name">
password: <input type="password" name="pwd">
<input type="submit" name="submit" value="login">
</form>
</body>
</html>
<?php
print_r($GLOBALS)
?>
在表单中输入sager, king,点击login
Array ( [_POST] => Array ( [name] => sager [pwd] => king [submit] => login )
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<form action="#" method="GET">
<input type="text" name="id">
<input type="submit" name="submit" value="提交">
</form>
</body>
</html>
<?php
print_r($_REQUEST); // $_REQUEST可以接受GET和POST方法发送的数据
?>
$_SERVER['PHP_SELF']
当前文件自身
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<!--#代表提交到当前数据-->
<!--在action的字符串值中,写入PHP代码-->
<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="POST">
用户:<input type="text" name="name"><br>
密码:<input type="password" name="pwd"><br>
<input type="submit" name="submit" value="登录">
</form>
</body>
</html>
<?php
// font是字体标签
echo "当前用户是:<font style='color: aquamarine'>".@$_POST['name'];
?>
预定义常量:
<?php
define("DEMO", true); // 常量
// 预定义常量:在php中通常都是以__开头__结尾,中间写常量名称,并且这是系统自带
echo "<br>当前代码的行号是:__LINE__:".__LINE__;
echo "<br>当前文件的路径和文件名:__FILE__:".__FILE__;
echo "<br>当前文件的路径:__DIR__:".__DIR__;
function add() {
echo "<br>当前定义的函数名是:__FUNCTION__:".__FUNCTION__;
}
add();
class Person
{
//属性、成员变量
var $name = "tcy";
var $age = 10;
//属性、成员函数
function FunctionName()
{
echo "<br>当前的类是:__CLASS__:".__CLASS__;
}
}
// 创建对象
$xiaoming = new Person();
// 在实例化之后,引入属性时,不要加$
$xiaoming->name = "xiaoming";
$xiaoming->age = 20;
$xiaoming->FunctionName();
?>
构造函数:主要用来在创建对象时初始化对象,为成员变量赋初始值,这意味着在new对象时用到该函数
析构函数:在当前页面中,对象使用完毕,页面加载完毕,需要销毁对象,会自动调用该函数
<?php
header("charset=utf-8");
class Car{
var $name;
var $color;
var $date;
// 构造函数,用于在new对象时对当前的对象进行初始化。在new对象时自动调用该函数
function __construct($p1, $p2, $p3){
$this->name = $p1;
$this->color = $p2;
$this->date = $p3;
}
// 析构函数:在当前页面中,对象使用完毕,页面加载完毕,需要销毁对象,会自动调用该函数
function __destruct() {
echo "<br>所有对象都被销毁完毕!";
}
function type(){
echo "车的名称是:".$this->name."<br>颜色是:".$this->color."<br>生产日期:".$this->date;
}
}
$BYD = new Car("BYD","blue","1999-08-07");
$BYD->type();
echo "<br>整个页面加载完毕之后,才会调用类的析构函数!!!"
?>
子类会继承父类的属性和方法。
<?php
header("charset=utf-8");
class Car
{
var $name;
var $color;
var $GenerateDate;
function __construct($p1,$p2,$p3)/*构造函数,用于在NEW对象时,对当前的对象进行初始化,还函数在new对象时是自动调用函数*/
{
$this->name=$p1;
$this->color=$p2;
$this->GenerateDate=$p3;
}
function Run()
{
echo "车的名称是:".$this->name.",颜色是:".$this->color.",该种车型的生产日期是:".$this->GenerateDate;
}
/*function __destruct(){析构函数,在当前页面中对象使用完毕,页面加载完毕,需要销毁对象,会自动调用该函数
echo "\n"."所有对象都被销毁完毕!!!";
}*/
}
/**
* 定义的类是child_car,是Car类的子类,就就意味着会继承Car的属性和方法
*/
class child_car extends Car
{
var $car_qu;
function __construct($pa){
$this->car_qu=$pa;
}
function echoInfo()
{
echo ",该车的驱动方式是:".$this->car_qu;
}
}
$byd=new Car("BYD","red","2020-01-01");
$byd->Run();
echo "\n";
$car2=new child_car("后驱");
$car2->name="BMW";
$car2->color="black";
$car2->GenerateDate="1991-09-08";
$car2->Run();
$car2->echoInfo();
?>
表单操作
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<script>
function submit() {
document.getElementById("form").submit();
}
</script>
</head>
<body>
<form action="demo8.php" method="POST">
username: <input type="text" name="name"><br>
password: <input type="password" name="passwd"><br>
<button οnclick="submit()">提交数据</button>
</form>
</body>
</html>
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<a href="demo8.html">点击返回首页</a>
</body>
</html>
<?php
echo "username is: ".$_POST["name"]."<br>";
echo "password is: ".$_POST["passwd"]."<br>";
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script>
function autoSubmit() {
document.getElementById("form").submit();
}
</script>
</head>
<body>
<form id="form" action="demo9.php" method="get">
<select name="select" id="" οnchange="autoSubmit()">
<option value="">请选择教程</option>
<option value="https://cn.bing.com/">Bing</option>
<option value="https://www.baidu.com/">Baidu</option>
<option value="https://zh.wikipedia.org/wiki/Wikipedia:%E9%A6%96%E9%A1%B5">Wiki</option>
</select>
</form>
</body>
</html>
<?php
header("charset=utf-8");
$url = $_GET["select"];
echo "访问的URL是:".$url;
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script>
function autoSubmit() {
document.getElementById("form").submit();
}
</script>
</head>
<body>
<form id="form" action="demo9.php" method="get">
<select name="select" id="" οnchange="autoSubmit()">
<option value="">请选择教程</option>
<option value="https://cn.bing.com/">Bing</option>
<option value="https://www.baidu.com/">Baidu</option>
<option value="https://zh.wikipedia.org/wiki/Wikipedia:%E9%A6%96%E9%A1%B5">Wiki</option>
</select>
</form>
</body>
</html>
<?php
header("charset=utf-8");
$url = $_GET["select"];
echo "访问的URL是:".$url;
echo "<iframe src='".$url."' frameborder='0'></iframe>";
<input type="checkbox" name="like[]">
checkbox是复选框,name的值是一个数组,点击提交按钮后,我们可以在URL中看到
http://localhost:63342/course/demo10.php?like%5B%5D=%E7%AF%AE%E7%90%83&like%5B%5D=%E6%8E%92%E7%90%83&submit=%E6%8F%90%E4%BA%A4
进行URL解码之后,是
http://localhost:63342/course/demo10.php?like[]=篮球&like[]=排球&submit=提交
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form action="demo10.php" method="get">
请选择你的爱好:<br>
<input type="checkbox" name="like[]" value="篮球">篮球 <br>
<input type="checkbox" name="like[]" value="足球">足球 <br>
<input type="checkbox" name="like[]" value="排球">排球 <br>
<input type="submit" name="submit" value="提交">
</form>
</body>
</html>
<?php
header("charset=utf-8");
$like=isset($_GET["like"])?$_GET["like"]:"";
if (is_array($like)) {
foreach ($like as $key => $value) {
// 下标key是int型的
echo "我的第".++$key."个爱好是".$value."<br>";
}
}
留言板v1.0
login.html
登陆界面
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>login</title>
</head>
<body>
<div align="center">
<p>username and password login</p>
<form action="login.php" method="post">
<p>
<input type="text" name="username" value="用户名">
</p>
<p>
<input type="password" name="password" value="密码">
</p>
<p>
<input type="submit" value="登录">
</p>
</form>
</div>
</body>
</html>
login.php
登陆检查界面
<?php
// 这里必须写成当前目录的相对路径形式./
include './info.php';
if ( ( isset($_POST[ 'username' ] ) && $_POST[ 'username' ] ) && isset ( $_POST["password"] ) && $_POST[ 'password' ]) {
// Sanitise username input
$user = $_POST[ 'username' ];
// $user = stripslashes( $usr );
// Sanitise password input
$pass = $_POST[ 'password' ];
// $pass = stripslashes( $pass );
// Check the info.php (if username matches the password)
if ( ( $user === $username ) && ( $pass === $password )) {
// Loin successful
// https://www.php.cn/php-weizijiaocheng-391661.html
header("location: logon.php");
exit;
} else {
// Login failed
sleep( rand( 2, 4 ) );
// Give the usr some feedback
print <<<EOT
<pre><br>Username and/or password incorrect.<br/></pre>
<a href="./login.html">账号或密码错误,请重新输入</a>
EOT;
}
} else {
// Username and/or password is empty
sleep( rand( 2, 4 ) );
// Give the usr some feedback
print <<<EOT
<pre><br>Username and/or password is empty.<br/></pre>
<a href="./login.html">请您输入用户名/密码</a>
EOT;
}
info.php
存储用户账号密码
<?php
$username = 'kali';
$password = 'linux';
?>
config.php
存储需要在logon.php
中调用的函数
<?php
// &是引用方式,如果该函数被调用,里面的参数会被原地修改
// 这里只需要用到'title'和'body'这两个键,其中'body'会被logon.php存入评论框和需要回显的评论内容
function &prisonPageNewGrab() {
$returnArray = array(
'title' => '四川监狱留言板',
'title_separator' => ' :: ',
'body' => '',
'page_id' => '',
'help_button' => '',
'source_button' => '',
);
return $returnArray;
}
function prisonHtmlEcho( $pPage ) {
print <<<EOT
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>四川监狱留言板</title>
</head>
<body>
<div id=\"main_body\">
<!--将logon.php中的'body'部分插入到这里-->
{$pPage[ 'body' ]}
<br /><br />
</div>
</body>
</html>
EOT;
}
?>
logon.php
登录成功之后的留言板界面
<?php
// 包含config.php,里面有我们当前页面需要调用的&prisonPageNewGrab()和prisonHtmlEcho( $pPage )函数
require_once './config.php';
// 新建一个$page数组,里面包含键'title'、'body'等,我们将用这个数组存储html页面
$page = prisonPageNewGrab();
$page[ 'title' ] = 'Sichuan Prison';
// 这里必须对$html进行初始化,赋值为空字符串。$html中存储的是用户在输入框中输入的内容,会嵌入到<pre>标签中
$html = "";
// Is there any input?
if( array_key_exists( "name", $_GET ) && $_GET[ 'name' ] != NULL ) {
// Get input,用户输入的内容
$name = htmlspecialchars( $_GET[ 'name' ] );
// Feedback for end user,将用户输入的内容插入<pre>标签中,再将插入内容的标签赋值给$html
$html .= "<pre>${name}</pre>";
}
$page[ 'body' ] .= "
<div>
<h1>Sichuan Prison MessageBox</h1>
<div>
<form name=\"XSS\" action=\"#\" method=\"GET\">
<p>
友善的评论是交流的起点
<input type=\"text\" name=\"name\">
<input type=\"submit\" value=\"Submit\">
</p>
</form>
{$html} <!--将插入了用户输入内容的pre标签插入在这里 -->
</div>
</div>\n";
// 输出整个HTML页面,HTML页面的主体部分在config.php的prisonHtmlEcho( $pPage )中定义,这里只需要'body'部分插入主体
prisonHtmlEcho( $page );
?>