PHP variables Getting Started tutorial (4) external variables for PHP

Source: Internet
Author: User
Tags html form setcookie

External variables for PHP

HTML forms (GET and POST)

When a single table is handed to a PHP script, the information in the form is automatically available in the script. There are many ways to access this information, such as:

A simple HTML form

<form action= "foo.php" method= "POST" >    Name:  <input type= "text" name= "username" >    Email: < Input type= "text" name= "email" >    <input type= "Submit" name= "submit" value= "Submit me!" ></form>

There are many ways to access the data in an HTML form, depending on your specific settings and personal preferences. For example:

Access data from a simple POST HTML form

<?php//from PHP 4.1.0 available   print $_post[' username '];   Print $_request[' username '];   Import_request_variables (' P ', ' p_ ');   Print $p _username;//available from PHP 3. Since PHP 5.0.0, these longer pre-defined variables//can be closed with register_long_arrays directives.   print $HTTP _post_vars[' username '];//if PHP command register_globals = On is available. But since//PHP 4.2.0 The default value is Register_globals = off. The use/Reliance of this method is not advocated.   print $username;? >

Using get forms is similar, except to use the appropriate get pre-defined variables. GET also applies to query_string (URL in the "?" After the information). So, for example, http://www.example.com/test.php?id=3 contains get data that can be accessed using $_get[' ID '. See $_request and Import_request_variables ().

Note: Arrays of hyper-global variables, like $_post and $_get, are available from PHP 4.1.0.

As shown, the default value of Register_globals before PHP 4.2.0 is on. In PHP 3, its value is always on. The PHP community encourages you not to rely on this directive and recommends that it be off when encoded.

Note: The MAGIC_QUOTES_GPC configuration directive affects the values of get,post and cookies. If turned on, the value (It's "php!") is automatically converted (it\ ' s \ "Php!\"). The insertion of the database needs to be escaped. See Addslashes (), Stripslashes (), and Magic_quotes_sybase.

PHP also understands the array in the context of a form variable (see Related FAQs). For example, you can group related variables, or use this attribute to get a value from a multiple-selection input box. For example, POST a form to yourself and display the data on commit:

More complex form variables

<?phpif (Isset ($_post[' action]) && $_post[' action '] = = ' submitted ') {print ' <pre> ';p rint_r ($_post); print ' <a href= '. $_server[' php_self '). ' " >please try Again</a> ';p rint ' </pre> ';} else {><form action= "<?php echo $_server[' php_self '];?>" method= "POST" >name:  

In PHP 3, the array used in variables is limited to one-dimensional arrays. In PHP 4, there is no such restriction.

IMAGE SUBMIT Variable Name

When submitting a form, you can use an image instead of the standard submit button, using a tag like this:

<input type= "image" Src= "Image.gif" name= "sub" >

When the user clicks somewhere in the image, the corresponding form is routed to the server, plus two variables sub_x and sub_y. They contain the coordinates of the user's click image. Experienced users may notice that the actual variable name sent by the browser contains a point rather than an underscore, but PHP automatically converts the point to an underscore.

HTTP Cookies

PHP transparently supports HTTP cookies in the Netscape specification definition. Cookies are a mechanism for storing data in a remote browser and for tracking or identifying users who are re-visited. Cookies can be set using the Setcookie () function. Cookies are part of the HTTP message header, so the Setcookie function must be called before any output is sent to the browser. The same restrictions apply to the header () function. Cookie data is available in an array of corresponding cookie data, such as $_cookie, $HTTP _cookie_vars and $_request. See the Setcookie () manual page for more details and examples.

If you want to assign multiple values to a cookie variable, you must assign it to a group. For example:

<?phpsetcookie ("Mycookie[foo]", "Testing 1", Time () +3600), Setcookie ("Mycookie[bar", "Testing 2", Time () +3600);? >

This will create two separate cookies, although MyCookie is a single array in the script. If you want to set multiple values in just one cookie, consider using serialize () or explode () on the value first.

Note that a cookie in the browser replaces a cookie with the same name, unless the path or domain is different. Therefore, for the shopping box program you can keep a counter and pass it together.

An example of a setcookie ()

<?phpif (Isset ($_cookie[' count ')) {$count = $_cookie[' count '] + 1;} else {$count = 1;} Setcookie ("Count", $count, Time () + 3600), Setcookie ("cart[$count]", $item, Time () + 3600);? >

The point in the variable name

Typically, PHP does not change the name of the variable passed to the script. However, it should be noted that the point (Dot,period,full stop) is not a valid character in the PHP variable name. As for the reasons, look at:

<?php$varname.ext;  /* Illegal variable name */?>

At this point, the parser sees a variable named $varname followed by a string join operator followed by a bare string (such as a string without quotes and does not match any known health or reserved words) ' ext '. Obviously this is not the desired result.

For this reason, be aware that PHP will automatically replace the points in the variable name with an underscore.

Determining variable Types

Because PHP determines the type of the variable and transforms it when needed (usually), it is not obvious what type of variable is given at a certain point in time. PHP includes several functions that can determine the type of variable, for example:GetType (),Is_array (),is_float (),is_int (),Is_ Object () and is_string (). See Chapter type.

PHP variables Getting Started tutorial (4) external variables for PHP

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.