What is a variable from outside of PHP

Source: Internet
Author: User
Tags setcookie

HTML forms (GET and POST)

When a form is submitted 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:

Example #1 a simple HTML form

<form action= "foo.php" method= "POST" >    Name:  <input type= "text" name= "username" ><br/>    Email: <input type= "text" name= "Email" ><br/> <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:

Example #2 Access data from a simple POST HTML form

<?php//from PHP 4.1.0 available with   echo $_post[' username '];   echo $_request[' username '];      Import_request_variables (' P ', ' p_ ');   echo $p _username;//from PHP 5.0.0, these long-form predefined variables//can be closed with register_long_arrays directives.   echo $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.   echo $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 the get data that can be accessed by using $_get[' ID '). See $_request and Import_request_variables ().

Note:

Hyper-global arrays such as $_post and $_get are available from PHP 4.1.0.

Note:

The points and spaces in the variable name are converted to underscores. For example <input name= "a.b"/> into $_request["A_b"].

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

Note:

MAGIC_QUOTES_GPC configuration directives affect 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 more than 10 years ago needs to be so escaped that it is now obsolete and should be closed. 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:

Example #3 more complex form variables

<?phpif (Isset ($_post [' action]) && $_post [' action '] = = ' Submitte     d ') {echo ' <pre> ';    Print_r ($_post);  Echo ' <a href= '. $_server [' php_self '].    ' ">please try Again</a> '; Echo ' </pre> ';}  else {><form action= "<?php echo $_server [' php_self ']; ?> "method=" POST "> Name: <input type=" text "Name=" personal[name] "><br/> Email: <input type=" t  Ext "name=" personal[email] "><br/> Beer: <br> <select multiple Name=" beer[] "> <option  Value= "Warthog" >Warthog</option> <option value= "Guinness" >Guinness</option> <option Value= "Stuttgarter" >stuttgarter schwabenbr</option> </select><br/> <input type= "hidden" NA Me= "Action" value= "submitted"/> <input type= "Submit" name= "submit" value= "Submit me!"/></form><?ph P}?> 

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 names sent by the browser contain a point rather than an underscore (that is, sub.x and sub.y), but PHP automatically converts the dots to underscores.

HTTP Cookies

PHP transparently supports HTTP cookies in the» RFC 6265 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:

<?php  Setcookie ("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. So for the shopping cart program you can keep a counter and pass it together, for example:

Example #4 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 (period) is not a valid character in the PHP variable name. As for the reasons, look at:

<?php$varname. ext;   ? >

At this point, the parser sees a variable named $varname followed by a string join operator followed by a bare string (that is, 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 ()

Related Article

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.