Cookies and session s:
Difference between cookie and session: Cookie data exists locally, while session data exists on the server side. Session is more secure than cookie.
cookie: it is used to remember what the client has done during the http session. It can solve the problems such as advertising on the first landing page. The server and the client are stateless when developing: it means that the two do not know each other after the interaction between the server and the client. Its use is as follows:
//Set cookie s and session s in php: //1. Set the set cookie in the response header, which can be identified to the client. The header ('set Cookie: key = value ') will be overwritten when the same key is set, for example: header('Set-Cookie: notice = password'); header('Set-Cookie: notice1 = password1'); //2. There is a special function to set cookie s in php, which can pass in multiple parameters: the third parameter is the expiration time stamp, and the third parameter is the session level session by default (it will be deleted automatically after closing the browser). setcookie('key');--------Only one parameter passed is delete setcookie('key','value',time()+1*24*60*60);-------The time here cannot be the current time, because the time is always updated, so the time should be longer, otherwise cookie Will be deleted immediately. Fourth parameter path Yes, set the scope of action path: setcookie('key','value',time()+1*24*60*60,'/demo');-----www.happy.com/images/1.jpg www.happy.com/demo/index.html,Indicates that under such a domain name, access/demo The address below is cookie,visit/demo There are no paths at the upper level or at the same level cookie. Fifth parameter domain set up cookie Scope of domain name:------If set domain For: day-01.io,day-01.io So in www.day-01.io This domain name is accessible,stay www.day-02.io This domain name cannot be accessed. setcookie('key','value',time()+1*24*60*60,'/demo','day-01.io'); Sixth parameter secure Yes, only https Can be used. The value is Boolean: setcookie('key','value',time()+1*24*60*60,'/demo','day-01.io',false); Seventh parameter httponly Yes, only the server can operate cookie,JS No operation cookie Values are Boolean: setcookie('key','value',time()+1*24*60*60,'/demo','day-01.io',false true); 3.stay php Middle take cookie: $_COOKIE,The result is an associated data var_dump($_COOKIE); 4.JavaScript operation cookie: obtain: document.cookie set up: document.coolie='js=fuck',This is not an overlay copy, but an incremental setting. JS operation cookie Here are special extensions that will js-cookie: https://github.com/js-cookie 5.cookie There are potential safety hazards. It is generally used here session['key']='value';stay php Medium operation session There is performance loss, so it needs to be turned on session: Open configuration file or code: session_start(); set up session: $_SESSION['key']='value'; obtain session: $_SESSION; //Principle of closing advertisement: when setting and obtaining session or cookie is completed, judge whether the value of a session or cookie exists to control the display or hiding of a part of the page, such as: //Ad page: <?php if(empty($_COOKIE['hide_adv'])||$_COOKIE['hide_adv']!=='1'): ?> <div class='adv'> <a href="index.php?action=close_adv" class='aa'>Don't show again</a> </div> <?php endif ?> //Turn off ad processing: <?php if(isset($_GET['action'])&&$_GET['action']==='close_adv'){ setcookie('hide_adv','1'); $_COOKIE['hide_adv']==='1'; }; ?>
Generation of dynamic web pages:
So far, we can put the static web page on the server, and the client requests the website through the domain name, but for us, what Apache does is too simple, Nothing more than requesting the corresponding file - reading the file - responding the file content to the client browser (the file is given to the user intact, which can not meet the need to make the content of the web page move and change dynamically with the data). So someone put forward the concept of server-side dynamic web page. There are many technologies that can realize this concept: JSP and ASP Net PHP node, etc. The principle of these technologies is not to write HTML fixedly. Each time the user requests, the code will be executed dynamically to temporarily generate an HTML page that the client wants.
web server workflow:
1. Judge whether it is a static file according to the file type corresponding to the requested URL
2. If the request is a static file, directly read the contents of the file and return the contents of the file to the client
3. If the requested file is not a static file, read the file code, execute this code (give it to other programs for execution, such as php), and return the string obtained from the execution result to the client.
Configure php extensions for Apache:
Because Apache can't handle dynamic web pages, it needs php to complete the development of dynamic web pages. So how to connect php and Apache? The specific process is as follows:
1.First, you need to Apache Configuration file for httpd.conf Custom write in file php stay Apache Module configuration instructions in: loadModule Custom name_module C:/php Installation path/php7apache2_4.dall,For example: LoadModule php7_module C:/F/php/php7apache2_4.dll 2.Continue in Apache Add this instruction to: AddType application/x-httpd-php .php To specify php This module does not judge whether to use the suffix according to the media type php Work, but according to MIME Type Judge whether it is php work 3.Configure the top Apache After configuring the file, go to php Official website: https://www.php.net/downloads after downloading the PHP compressed package, unzip the PHP compressed package to a directory. Note that it is the same as httpd The path in conf configuration is consistent; Note: some servers support PHP and can be used directly without downloading the PHP installation package. Tip: for beginners or developers who do not want to configure the environment, you can use the integrated environment for fool installation. For details, please read the official documents.
Apache and php:
For many beginners, it's easy to mix Apache and php together. In fact, they have their own areas of responsibility and responsibilities, but when we use php to make dynamic web pages, both of them will have a relationship:
Client request - Apache judges whether it is a static web page. If so, it processes it by itself, reads the file, and responds to the result to the client; Otherwise, give it to php to read the code in the php file and execute the code, return the executed code to Apache, and then present it to the client.
php introduction:
After the above brief introduction, I have a preliminary understanding of PHP. If I want to learn PHP well, I have to continue to refer to the official documents to learn relevant API s. For details, please refer to the following documents: http://php.net/manual/zh/index.php
The value of php: get some specified data by executing php code and fill it into the specified location of html
php(PHP:Hyper Preprocessor) is a widely used scripting language. It can be embedded into HTML, especially suitable for dynamic website development. The characteristics of php are similar to other script sounds, including variables, functions, loops, and so on. The grammar is different, but the concept is basically the same. The main function of using php is to write dynamic web pages. The core of dynamic is to make HTML no longer write dead, but to achieve dynamic purpose by embedding a piece of code that can be executed on the server in HTML.
php is often used for the output instruction of test results: echo can print multiple parameters, print can only print one parameter, var_dump() output data type and data;
Tip: the pictures and other materials in this article come from the Internet. If there is infringement, please send an email to: 810665436@qq.com Contact the author to delete.
Author: Kuhai