PHP uses Memcache to store session other "reprint"

Source: Internet
Author: User
Tags memcached expression engine

PHP uses Memcache to store session

Category: PHP time: March 30, 2015

Many times a complete system may run on multiple servers, if the server needs to share the session, then the default PHP files to save the session is powerless. At this point we can consider using memcache to take over the session save and read work.

Method I: Global settings in php.ini
PHP.ini Code

1
2
Session. Save_handler = Memcache
Session. Save_path = "tcp://127.0.0.1:11211"

Method II: In a directory. htaccess
PHP.ini Code

1
2
Php_value session. Save_handler "Memcache"
Php_value session. Save_path "tcp://127.0.0.1:11211"

Method III: Again or in one application
PHP code

1
2
Ini_set("Session.save_handler", "Memcache");
Ini_set("Session.save_path", "tcp://127.0.0.1:11211");

Use multiple memcached servers separated by commas "," and, as described in the memcache::addserver () documentation, can take additional parameters "persistent", "weight", "timeout", "Retry_ Interval "And so on, like this:" Tcp://host1:port1?persistent=1&weight=2,tcp://host2:port2″.

If the installed pecl is memcached (which relies on the extension of the libmemcached library), the configuration should be
PHP code

1
2
Ini_set("Session.save_handler", "memcached"); //Is memcached not memcache
Ini_set("Session.save_path", "127.0.0.1:11211"); //Do not TCP
PHP Buffer Summary

Category: PHP time: December 29, 2014

In some loops executed statements, the browser does not have the expected output, what is the problem? This article mainly explains the PHP buffer. For PHP, every operation like the echo output is written to PHP buffer, and the data is displayed on the browser when the script is executed or the force output cache is executed.

Let's look at a piece of code first.

1
2
3
4
5
for  ( $i =10 ;  $i >0 $i --) {
echo   $i flush ( sleep (1 /span>

According to the PHP manual, the function sends all the output of the program so far to the user's browser. The above code should output $i once every second. But this is not necessarily the case in practice. It is possible that after waiting for 10 seconds, all the output is presented at the same time.

OK, let's change this code to

1
2
3
4
5
6
ob_end_clean (; Modify Section
for  ( $i = 10;  $i >0< Span class= "sy0" >;  $i --) {
echo  $i flush ( sleep (1 /span>

Hey, add this sentence Ob_end_clean (); it's OK. In fact, we can change Ob_end_clean () to Ob_end_flush () as well.

I'll change it again.

1
2
3
4
5
6
for  ( $i =10;  $i >0;  $i -- {
echo  $i ;
ob_flush (; Modify Section
flush ( sleep (1 /span>

Run, is it found that the $i also output once every second? What is this for?
Don't worry, let's take a look at php.ini.

Open php.ini, search for output_buffering, and we'll see settings like this output_buffering = 4096. As its name output_buffering, this setting is the function of buffering the output, the buffer size is 4096bytes.

In our first piece of code, the reason for not outputting as expected is that the output_buffering buffers the output. The output will not be sent out until the 4096bytes is reached or the script is finished.

The effect of Ob_end_clean () and Ob_end_flush () in the second piece of code is to terminate the buffer. This will not have to wait until there is a 4096bytes buffer to be sent out.

In the third code, a sentence of Ob_flush () is used to send the buffered data, but it does not terminate the buffer, so it must be used before each flush ().

If you do not want to use Ob_end_clean (), Ob_end_flush () and Ob_flush (), we must set the output_buffering in php.ini to be small enough, for example, to set to 0. It is important to note that if you are going to use Ini_set ("output_buffering", "0″") in your script to set it up, please stop, this method is not possible. Because at the beginning of the script, the buffering settings are already loaded, and then the buffering begins.

You might ask, since Ob_flush () is sending the buffered data, why flush () is required??? Can't you just use the following code?

1
2
3
4
5
for  ( $i =10 ;  $i >0 $i --) {
echo   $i ob_flush ( sleep (1 /span>

Please note the difference between ob_flush () and flush (). The former is the release of data from the buffer of PHP, the latter is not buffered or released data sent to the browser. So when the buffers are present, we have to use both ob_flush () and flush ().

Is that the flush () that is indispensable here? No, we have another way to get the data out and send it to the browser right away. The following two pieces of code do not need to use flush (). (When you set output_buffering to 0, even Ob_flush () and Ob_end_clean () are not required)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
Ob_implicit_flush(True);
For($i=10;$i>0;$i--){
Echo$i;
Ob_flush();#如果ob函数打开的情况下
Sleep(1) "


ob_end_clean ( ob_implicit_flush (true for  ( $i =10< Span class= "sy0" >;  $i >0; < Span class= "RE0" > $i --) {
echo < Span class= "RE0" > $i sleep (1 /span>

Note the above Ob_implicit_flush (true), which forces the output to be sent to the browser whenever there is an output. This does not require each output (echo) to be sent to the browser with flush ().

The above may not be true in some browsers. Because the browser also has its own rules. I used firefox1.5,ie6,opera8.5 to test it. Where opera does not output normally, because it has a rule that does not encounter an HTML tag, it is absolutely not output, unless the script ends. And Firefox and IE are relatively normal.

Finally, I enclose a very interesting code, the author is the Putty shell. In a script cycle, each output will overwrite the previous output.
The following code is only available under Firefox, and other browsers do not support Multipart/x-mixed-replace's content-type.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Header(' Content-type:multipart/x-mixed-replace;boundary=endofsection ');
Print"\n--endofsection\ n ";

$pmt=Array("-","\\","|","/");
For($i=0;$i<10;$i++){
Sleep(1);
Print"Content-type:text/plain\ n\ n ";
PrintThe part$i\ t ".$pmt[ $i  % 4]< Span class= "sy0" >;
print  "--endofsection\n" ;
ob_flush ( flush (
print  "Content-type:text/plain \n\n " print  "the End\n" print  "--endofsection--\n"
The use of regular expressions in PHP

Category: PHP time: November 28, 2014

Regular expressions are a very important point of knowledge in PHP, often used to find and replace strings, to verify that the information format that the user has entered conforms to specifications, such as message format, phone format, and so on. There are also collectors and the like in the software, the regular is also necessary! It mainly introduces several common regular functions in PHP: Preg_match (), Preg_match_all (), Preg_grep (), Preg_split ().

1.preg_match ()
This function is commonly used for form validation. You can search and match strings in the specified regular expression pattern. The function has two required parameters, the first parameter needs to provide a pattern written by the user in regular expression syntax, and the second parameter requires a string. If you provide a third optional array parameter, matches, you can have a matching result that holds the parts of the sub-pattern in the first argument.

2.preg_match_all ()
This function is similar to the Preg_match () function, unlike the function preg_match () that stops searching after the first match. The function Preg_match_all () will always search to the end of the specified string, and can get all matching results.
This function puts all possible matching results into the array of the third parameter, returns the number of times the pattern matches, and returns False if an error occurs. If the fourth argument is used, the matching result of each occurrence is saved to the array of the third parameter, according to the order in which it is specified. The fourth parameter has two predefined values
A.preg_pattern_order: It is the default value of the Preg_match_all () function, sorting the result to make $matches[0] an array of all pattern matches, $matches [1] is an array of strings that match the sub-patterns in the first parenthesis, And so on
B.preg_set_order: The result sort is an array of $matches[0] for the first set of matches, $matches [1] is an array of the second set of occurrences, and so on.

3.preg_grep ()
Unlike the first two functions, the function matches the elements in the array and returns the array cells that match the regular expression.
The function returns an array that contains the cells in the second parameter array that match the given first parameter pattern, which is vaguely matched for each element of the output array.

4.preg_replace ()
The function performs the search and substitution of regular expressions, and is one of the most powerful string substitution handler functions.
The function has three parameters, the first parameter matches, and two parameters are replaced. The third parameter to be searched more than one. If the fourth optional parameter limit is specified, only the limit match is replaced, and if the limit is given or the value is-1, all occurrences are replaced.

5.str_replace ()
This function is a PHP-enhanced string-handling function, and can also implement a replacement wage for strings. Although the substitution function with no regular expression is powerful, the substitution of some simple strings is more efficient than the preg_replace () function.
The function has a single required parameter, an optional parameter, the first parameter is the target object, the second parameter is the replacement object, the third is the processed string, the function in the string of the third argument, the case-sensitive way to search for the first parameter to improve the target object, Replaces all instances found with the replacement object provided by the second parameter. If the target object is not searched in the third argument, the processed string remains unchanged. The fourth is an optional argument, a reference to a variable that must pass in a variable name to save the number of substitutions.
The first two arguments using Str_replace () can not only use a string, but also make an array.

6.preg_split ()
The function uses a Perl-compatible regular expression syntax that can be used to split a string by the method of a regular expression.
The function returns a string array in which the element contains a string through the second argument, a regular expression of the first parameter, as a substring of the matched boundary. If a third string of limit is specified, a maximum of linmit substrings are returned. And the last element in it contains all the parts that are left after being split. If limit is-1, it means there is no limit. The fourth parameter is an optional parameter,
>> Preg_split_no_empty: If this tag is set, Preg_split () returns only non-empty components
>> preg_split_delim_capture: If this tag is set, the parentheses expression in the delimiter pattern is also captured and returned
>> preg_split_offset_capture: If this tag is set, the matching result for each occurrence also returns its subordinate string offset. Note that this changes the value of the returned array so that each cell in it is also an array, where the first item is the matching string, and the second item is its offset in the original string.

function explode ()
If you are splitting with only a particular string, we recommend using the explode () function, which does not have to call the regular expression engine, so the speed is the fastest.
The function has three parameters, the first parameter increases a split character or string, the second argument is a split string, and if the third optional parameter limit is provided, specifies the maximum number of substrings to be split into. The function returns an array of separated substrings.

function implode ()
corresponding to the split string is the implode () function, which combines all the elements in the array into a single string. The function join () is the alias of the function.
The function has two parameters, the first parameter raises a link character or string, and the second parameter specifies a linked array.

PHP uses Memcache to store session other "reprint"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.