php core technology learning

1. Cookie and session
①Create cookie, read cookie, and obtain cookie characteristics

//test1.php
<?php
	setcookie("user[name]","admin",time()+600);
	setcookie("user[pw]","123456",time()+600);
	setcookie("user[city]","china",time()+600);
	echo '创建Cookie信息成功,<a href="test2.php">点击查看Cookie信息</a>';
?>
 
//test2.php
<?php
	echo "Cookie信息:<br/>";
	if(isset($_COOKIE['user']))//判断是否存在cookie的状况特点
	{
    
    
		foreach($_COOKIE['user'] as $key=>$value){
    
    
			echo $key."=>".$value."<br/>";
		}
		echo '<a href="test3.php">删除Cookie信息</a>';
	}
	else{
    
    
		var_dump($_COOKIE);//输出所有的变量
	}
?>
 
//test3.php
<?php
	setcookie('user[name]',"");
	setcookie('user[pw]',"123456",time()-1);//利用时间删除cookie
	setcookie('user[city]',"china",time()-1);
	echo '已清空Cooike信息,<a href="test2.php">返回查看Cookie信息</a>';
?>

②Session management

  1. session create session
<?php
session_start();//启动session的初始化
$_SESSION["username"]="skygao";//注册session变量,赋值为一个用户的名称
$_SESSION["uid"]=1;//注册session变量,赋值为一个用户的ID
?>

  1. Session delete session
    ① Determine whether there is a session in the cookie and delete it
<?php
if(isset($_COOKIE[session_name()]))//判断Cookie中是否存在session ID
{
    
    
  
    setcookie(session_name(),'',time()-3600,'/');  //删除包含Session ID的cookie,注意第四个参数一定要和php.ini设置的路径相同
}
?>
//方法二
//删除单个会话unset($_SEIION[‘user’]);
//删除多个会话,即直接将一个空的数组赋值给$_SESSION$_SESSION=array();
③结束当前的会话
session_destory();

③Comprehensive application

<?php
session_start();//第一步:开启Session并初始化
$_SESSION = array();删除所有Session的变量,也可以用unset($_SESSION[XXX])逐个删除


if(isset($_COOKIE[session_name()])) //如果使用基于Cookie的session,使用setCookkie()删除包含Session ID的cookie
{
    
    
    setCookie(session_name(), "", time()-42000, "/");
}
session_destroy();//第四部:彻底销毁session

?>


2. File system processing①File
processing

  1. Open and close files
<?php
$myfile = fopen("webdictionary.txt", "r");//c语言中利用的指针进行直接打开

fclose($myfile);//文件关闭
?>

  1. Read and write files
    Read a line of data or read a character
//遍历文件处理
while(!feof($myfile))
{
    
    
   echo fgets($myfile) . "<br>";
}//即一次读取一行数据+利用while函数实现读取整个文件
  
while(!feof($myfile))
{
    
    
   echo fgetc($myfile);
} //读取一个字符思路

//任意长度字串读取
fread($fp,32);
//即相互$fp的(即读取的文件,32为需要读取的字节个数)

② Directory processing

  1. Open/close directory
<?php
//打开目录
$path="../";
if(is_dir($path)){
    
    
if($dire=opendir($path))//opendir($path)即打开目录函数
echo $dire;
}else{
    
    
echo "路径错误";
exit();
}
//....  //其他操作
 
closedir($dire);//closedir()即关闭目录函数
?>
  1. Browse the catalog
$path="../test";
if( is_dir($path) ) //is_dir()函数用于判断当前路径是否是一个合法的目录。如果合法,返回true,否则返回false。
{
    
    
$dir=scandir($path);//scandir函数取得所有文件及目录
foreach ($dir as $value) //使用foreach循环
    {
    
    
echo $value."<br>";
    }
}
else
{
    
    
echo "目录路径错误";
}
  1. File upload
    (requires two modules)
    Module 1: Front-end code construction submission interface
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8"/>
<title>上传你的File吧</title>
</head>
<form enctype="multipart/form-data" method="post" action="uploadprocess.php">
<table>
<tr><td align="center" colspan="2"><font style="font-size:40px;font-family: 华文彩云;">文件上传</font></td></tr>
<tr><td>请填写用户名:</td><td><input type="text" name="username"/></td></tr>
<tr><td>请简单介绍该文件</td><td><textarea name="fileintro" rows="10" cols="80"></textarea></td></tr>
<tr><td>请选择你要上传文件:</td><td><input type="file" name="myfile"/></td></tr>
<tr><td><input type="submit" value="上传文件"/></td><td></td></tr>
</table>
</form>
</html>

Module 2: Back-end code
written by other big guys called by the code block, here to learn

<?php
	//1.接收提交文件的用户
	$username=$_POST['username'];
	$fileintro=$_POST['fileintro'];
 
	//我们这里需要使用到 $_FILES
	/*echo "<pre>";
	print_r($_FILES);
	echo "</pre>";*/
 
	//其实我们在上传文件时,点击上传后,数据由http协议先发送到apache服务器那边,这里apache服务器已经将上传的文件存放到了服务器下的C:\windows\Temp目录下了。这时我们只需转存到我们需要存放的目录即可。
 
	//php中自身对上传的文件大小存在限制默认为2M
	
	//获取文件的大小
	$file_size=$_FILES['myfile']['size'];
	if($file_size>2*1024*1024) {
    
    
		echo "文件过大,不能上传大于2M的文件";
		exit();
	}
 
	$file_type=$_FILES['myfile']['type'];
	echo $file_type;
	if($file_type!="image/jpeg" && $file_type!='image/pjpeg') {
    
    
		echo "文件类型只能为jpg格式";
		exit();
	}
 
 
	//判断是否上传成功(是否使用post方式上传)
	if(is_uploaded_file($_FILES['myfile']['tmp_name'])) //即上传前的名字,上传后的命名名字
	{
    
    
		//把文件转存到你希望的目录(不要使用copy函数)
		$uploaded_file=$_FILES['myfile']['tmp_name'];
 
		//我们给每个用户动态的创建一个文件夹
		$user_path=$_SERVER['DOCUMENT_ROOT']."/studyphp/file/up/".$username;
		//判断该用户文件夹是否已经有这个文件夹
		if(!file_exists($user_path)) {
    
    
			mkdir($user_path);
		}
 
		//$move_to_file=$user_path."/".$_FILES['myfile']['name'];
		$file_true_name=$_FILES['myfile']['name'];
		$move_to_file=$user_path."/".time().rand(1,1000).substr($file_true_name,strrpos($file_true_name,"."));
		//echo "$uploaded_file   $move_to_file";
		if(move_uploaded_file($uploaded_file,iconv("utf-8","gb2312",$move_to_file))) {
    
    
			echo $_FILES['myfile']['name']."上传成功";
		} else {
    
    
			echo "上传失败";
		}
	} else {
    
    
		echo "上传失败";
	}
 
?>

Third, object-oriented
mind map
Insert picture description here
① Class definition + reference member + instantiation method

class Preson {
    
                                   //定义了一个Preson类

public $name = ‘aaa’;                //定义类的属性(姓名,性别,年龄等等)

public $age =2;

public $gender = ‘男’;

}

//new指创建一个新的人,并把这个新的对象赋值给$Preson1(实例化)

$Preson1 = new Preson();                 //实例化类

$Preson1->name = "张三";

$Preson1->age = 22;

$Preson1->gender = "女";

echo  $Preson1->name.'  '.$Preson1->age.'  '.$Preson1->gender;

//如果想输出第二个实例,直接输出就OK了,只要改一个变量名

$Preson2 = new Preson();                    //实例化类

$Preson2->name = "小亮";

$Preson2->age = 25;

$Preson2->gender = "男";

②Construction method and destruction method

<?php
class MyObeject
{
    
    
	public $name; // 姓名
	public $age;  // 年龄
	public $salary; // 薪水
	// 1.构造方法使用固定的方法名:__construct()
	public function __construct($name,$age,$salary)		// 构造方法:通常用来初始化对象中的属性
	{
    
    
		$this->name = $name;//this->即在类内部使用,调用状况特点 对象名->方法名字(即利用this简化对象名的使用)
		$this->age = $age;
		$this->salary = $salary;
	}

	public function __destruct()	// 2.析构方法:对象销毁时自动调用,没有参数,__destruct()
	{
    
    
		echo '当前变量被销毁了~';
	}
}
 
// 创建一个对象访问类中的属性
$stuffA = new Stuff('Tom',24,5000);
echo $stuffA->name;
echo '<pre>';
print_r($stuffA);
echo '</pre>';
//显示页面
// Stuff Object
// (
//     [name] => Tom
//     [age] => 24
//     [salary] => 5000
// )
 
// 销毁变量
unset($stuffA); // 当前变量被销毁了~
?>	

②:: Characteristics of the use of the class

  1. Used inside the definition of the class

<?php 

class Fruit {
    
     

    const CONST_VALUE = 'Fruit Color'; 

} 

  

class Apple extends Fruit 

{
    
     

    public static $color = 'Red';

 

    public static function doubleColon() {
    
    

        echo parent::CONST_VALUE . "\n";

        echo self::$color . "\n";

    }

}

 

Apple::doubleColon();

?>
  1. Used outside the definition of the class
<?php 

class Fruit 

{
    
     

    protected function showColor() {
    
     

        echo "Fruit::showColor()\n"; 

    } 

} 

  

class Apple extends Fruit

{
    
    

    // Override parent's definition

    public function showColor()

    {
    
    

        // But still call the parent function

        parent::showColor();

        echo "Apple::showColor()\n";

    }

}

 

$apple = new Apple();

$apple->showColor();

?>

Because learning php originally just wanted to break through the code auditing capabilities of the web (stuck on it for a long time), php has learned almost here, and then I went crazy to audit the code to improve my ability.

Is it the same for you who code?
Is the state in the previous second and the next second different?
Insert picture description here

Guess you like

Origin blog.csdn.net/qq_33942040/article/details/106594572