saltfish666 / saltfish666.github.io

my blog
0 stars 0 forks source link

PHP 学习笔记 #2

Open saltfish666 opened 6 years ago

saltfish666 commented 6 years ago

PHP手册 菜鸟教程

<?php 
$x = "Hello world!";
echo $x;
echo "<br>"; 
$x = 'Hello world!';
echo $x;
?>

PHP就是嵌入HTML的脚本,被PHP解释器动态加载。 感觉定位上没什么出彩,JS理论上也可以提供这种功能(等一个大牛实现JS脚本解释器),大多数编程语言都提供模板HTML功能,在看看PHP有什么特别的把。

*.php文件都会被PHP解释器过滤

saltfish666 commented 6 years ago

变量名前多了$符号,为什么要加? 目前只感觉在写模板字符串的时候比较省事。 echo $PATH linux也用$辨识变量或参数 https://blog.csdn.net/itofwanyifei/article/details/40450553 https://www.oschina.net/question/104691_161885

没有显示的变量声明,导致函数内变量和全局变量傻傻分布清,和Python一样不得不引入global 关键字,何必呢?

<?php
$x=5;
$y=10;

function myTest()
{
    global $x,$y;
    $y=$x+$y;
}

myTest();
echo $y; // 输出 15
?>
saltfish666 commented 6 years ago

echo 字符串原样插入html,在传给浏览器

为什么要使用EOF,测试几下和“”没什么区别啊,就是EOF内部可以包含“”吗。。 为什么要用??不解

saltfish666 commented 6 years ago
        <?php
        $x="Hello world!";
        /*$x=null;*/
        var_dump($x);
        $y = 234;
        var_dump($y)
        ?>

判断数据类型

saltfish666 commented 6 years ago
<?php
// 区分大小写的常量名
define("GREETING", "欢迎访问 Runoob.com");
echo GREETING;    // 输出 "欢迎访问 Runoob.com"
echo '<br>';
echo greeting;   // 输出 "greeting"
?>

其他编程语言从语言层面定义常量,php通过函数定义?

常量是全局的

<?php
define("GREETING", "欢迎访问 Runoob.com");

function myTest() {
    echo GREETING;
}

myTest();    // 输出 "欢迎访问 Runoob.com"
?>
<?php
        function myTest() {
            define("GREETING", "欢迎访问 Runoob.com");
        }
        echo GREETING;// 输出  GREETING
       myTest();
        echo GREETING;// 输出 "欢迎访问 Runoob.com"
        ?>
saltfish666 commented 6 years ago
<?php 
$txt1="Hello world!"; 
$txt2="What a nice day!"; 
echo $txt1 . " " . $txt2; // Hello world! What a nice day!
?>

没有吧在字符串拼接重载为+,更自然,但是和其他语言不同意。

超级全局变量

http://www.runoob.com/php/php-superglobals.html

魔术变量

http://www.runoob.com/php/php-magic-constant.html

语言层面提供的小技巧,难登C语言大雅之堂。

saltfish666 commented 6 years ago

函数

存在static函数静态变量

好吧,也是一种特性

saltfish666 commented 6 years ago

saltfish666 commented 6 years ago

表单和用户输入

表单处理

http://www.runoob.com/php/php-forms.html

submit后,浏览器将变表单数据提交到了 welcome.php,php内置的$_POST 可以接受表单信息

saltfish666 commented 6 years ago

数组、数据结构

http://www.runoob.com/php/php-arrays.html 关联数组不就是dictmaphttp://www.berlinix.com/php/array.php 我倒是想看看PHP怎么优化这么杂乱的数据结构!

不过这也就是了一个事实: 数组也可以理解为key-value GET请求等用到querystring的地方,只只支持key -value,就是一种约定,理论上也可以支持数组,哎,再说什么。。。

saltfish666 commented 6 years ago

内置变量

<?php

//查找全局变量的列表
function test() {
    $foo = "local variable";
    echo '$foo in global scope: ' . $GLOBALS['foo']. "\n";
    echo '$foo in current scope: ' . $foo . "\n";
}
$foo = "Example content";
test();

//查找GET请求中的参数     http:...?name=lishuai
echo 'Hello ' . htmlspecialchars($_GET["name"]) . '!';
echo 'Hello ' .$_GET["name"]. '!';

//POST 请求
echo 'Hello ' . htmlspecialchars($_POST["name"]) . '!';

//查看cookies
echo 'Hello Cookie' . htmlspecialchars($_COOKIE["test"]) . '!';

$_SERVER

<?php

foreach ($_SERVER as $key => $value){
    echo "$key = $value <br/>";
}

除了http请求头,还有Apache服务器的信息。Apache服务器会把一些数据封装到这个数组中

HTTP_ACCEPT = text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8 
HTTP_USER_AGENT = Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36 
HTTP_HOST = localhost:63342 
CONTENT_TYPE = 
CONTENT_LENGTH = 0 
QUERY_STRING = _ijt=gl42ac7f00o4hh2mu9dhgrhtsv&name=sdfsd 
DOCUMENT_URI = /htdocs/hi/hi.php 
REDIRECT_STATUS = 200 
SERVER_PROTOCOL = HTTP/1.1 
GATEWAY_INTERFACE = CGI/1.1 
SERVER_PORT = 63342 
SERVER_ADDR = 127.0.0.1 
SERVER_NAME = PhpStorm 2017.3.3 
SERVER_SOFTWARE = PhpStorm 2017.3.3 
REMOTE_PORT = 58270 
REMOTE_ADDR = 127.0.0.1 
REQUEST_METHOD = GET 
REQUEST_URI = /htdocs/hi/hi.php?_ijt=gl42ac7f00o4hh2mu9dhgrhtsv&name=sdfsd 
FCGI_ROLE = RESPONDER 
PHP_SELF = /htdocs/hi/hi.php 
REQUEST_TIME_FLOAT = 1522589256.2831 
REQUEST_TIME = 1522589256 
argv = Array 
...

可以通过 REMOTE_ADDR = 127.0.0.1 封杀IP地址。

<?php
if($_SERVER['REMOTE_ADDR'] == "127.0.0.1"){
    header("Location:err.php");  //当前url(还是目录)下的err.php
}
saltfish666 commented 6 years ago

saltfish666 commented 6 years ago

控制refer

<?php
echo $_SERVER['HTTP_REFERER']."-- -- --";

/*echo "-----";
 $location = strpos($_SERVER['HTTP_REFERER'], "http://localhost:63342/htdocs/hi/hi.php");
echo $location;
echo false;  //打印不出来!!!!salimujiba false不能toString!!!
*/

/*echo strpos($_SERVER['HTTP_REFERER'], "http://localhost:63342");*/

echo strpos($_SERVER['HTTP_REFERER'], "http://localhost:63342/htdocs/hi");

if($_SERVER['HTTP_REFERER']){
    if(strpos($_SERVER['HTTP_REFERER'], "http://localhost:63342/htdocs/hi") === 0){//原来这里!!!

        echo "you are welcome";
    }
    else{
        echo "you are banned!!!";
    }
}else{
    echo "you are welcomehahahah";
}

两个坑:

saltfish666 commented 6 years ago

下载文件

<?php
echo "hi";
$file_name = "d.jpg";
if(!file_exists($file_name)){
    echo "not exit";
}
$fp = fopen($file_name,"r");
$file_size =filesize($fp);

header("Content-type:application/octet-stream"); //告诉浏览器文件类型
header("Accept-Ranges:bytes");    //按照字节为单位
header("Accept-Length:$file_size");  //文件大小
header("Content-Disposition:attachment;filename=$file_name");、、

浏览器发送请求后,直接下载,观察不到具体流程,就用postman测试

//header
accept-ranges →bytes
content-length →51
content-type →application/octet-stream
date →Monday, 02-Apr-18 01:19:53 CST
keep-alive →timeout=38
server →PhpStorm 2017.3.3
x-powered-by →PHP/7.2.1

//body
Content-Disposition:attachment;filename=d.jpg

hi

我估计Chrome看到这个页面后,立刻关闭当前标签页并向请求。。。。d.jpg文件。 上面设定的请求头就是下载文件的约束。

可是这样的话PHP文件中不需要打开文件啊。。。请求头也应该不需要写那么详细啊(可以再第二次报文中声明啊)。

saltfish666 commented 6 years ago
<?php
phpinfo();

生成查看PHP配置的页面。

saltfish666 commented 6 years ago

连接MySQL

http://www.w3school.com.cn/php/php_mysql_connect.asp

<?php

$conn = mysqli_connect("localhost:3306","zhugeliang","zhugeliang");

if($conn){
    echo "hi";
}else{
    echo "is wrong";
}
?>

操作数据库

http://www.runoob.com/php/php-mysql-select.html 就是执行SQL语句来操作。

<?php

$conn = mysqli_connect("localhost:3306","root","926443","phpDB");

/*if($conn){
    echo "hi";
}else{
    echo "is wrong";
}*/

// 创建数据库
/*$sql = "CREATE DATABASE phpDB";
if ($conn->query($sql) === TRUE) {
    echo "数据库创建成功";
    //命令行查看,确实成功
} else {
    echo "Error creating database: " . $conn->error;
    //Error creating database: Access denied for user 'zhugeliang'@'%' to database 'phpdb'
    //应该是权限不够
}*/

// 使用 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;
}*/

/*$sql = "INSERT INTO MyGuests (firstname, lastname, email)
VALUES ('Jon', 'Noe', 'jdsn@example.com')";

if (mysqli_query($conn, $sql)) {
    echo "新记录插入成功";
} else {
    echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}*/

//方法 fetch_assoc() 将结合集放入到关联数组并循环输出 就是弹出数据喽。
$sql = "SELECT id, firstname, lastname FROM MyGuests";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // 输出数据
    while($row = $result->fetch_assoc()) {   //弹出列表,key-value访问
        echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "<br>";
    }
} else {
    echo "0 结果";
}

$conn->close();

?>
saltfish666 commented 6 years ago

cookie 与session

cookie

<?php

setCookie("name","lishuai",time()+3600);//必须有expire,不然无法setcookies。
//观察到响应头有这个字段 Set-Cookie: name=lishuai; expires=Mon, 02-Apr-2018 12:04:44 GMT; Max-Age=3600
//再次刷新,观察到请求cookie中有 name=lishuai;同一域名的请求都有cookies

echo "hi";
?>
<?php
echo $_COOKIE['name'];

session

<?php

session_start();
$_SESSION['shopcar'] = 3;

//响应头出现  Set-Cookie: PHPSESSID=7313473ac68ugq1cbbu5dc6q8f; path=/

echo "ok";

//再次请求cookies出现  PHPSESSID=7313473ac68ugq1cbbu5dc6q8f;

//因为保存在内存,值可以是对象等
<?php

session_start();  //必须先初始化
echo $_SESSION['shopcar'];