12c01-1.php
来自「介绍PHP5的给类型函数应用」· PHP 代码 · 共 54 行
PHP
54 行
<?php// This script does three functions based upon the input.// If it finds cookies are provided, it just displays the information.// If it finds a POST command sent to it, it turns those into cookies,// and then displays the same information// If it finds neither, it presents a form to receive the data with.// Check if we have both the cookies that we care for:if (isset($_COOKIE['firstname']) && isset($_COOKIE['zipcode'])) { // We found the cookies, so just print the standard output print_standard_output($_COOKIE['firstname'], $_COOKIE['zipcode']);}// Else, if we received a form POST with this data:elseif (isset($_POST['firstname']) && isset($_POST['zipcode'])) { // First we need to create these as cookies. // First create 'firstname', which will only last until the browser is // killed and only return to this subdirectory. setcookie('firstname', $_POST['firstname']); // Determine our subdomain without having to know it: $parts = explode('.', $_SERVER['HTTP_HOST']); $cp = count($parts); // If a 1 part hostname, like 'localhost', don't bother: if ($cp == 1) { $subdomain = ''; } // Otherwise, generate our subdomain by using the last 2 parts else { $subdomain = ".{$parts[$cp-2]}.{$parts[$cp-1]}"; } // Now create 'zipcode', which returns to our whole subdomain, to the // entire website, (from '/'), and which doesn't expire for 30 days. setcookie('zipcode', $_POST['zipcode'], time()+(30*24*3600), '/', $subdomain); // Now, also, just print the standard output: print_standard_output($_POST['firstname'], $_POST['zipcode']);}// We didn't have anything, so make a form so they can provide data.else { echo "<form action=\"{$_SERVER['PHP_SELF']}\" method=\"POST\"><p>What is your first name? <input type=\"text\" name=\"firstname\" /></p><p>What is your zipcode? <input type=\"text\" name=\"zipcode\" /></p><p><input type=\"submit\" /></p></form>";}// The standard output:function print_standard_output($name, $zip) { echo "<p>Welcome back {$name}! Perhaps you would like to see things happening near your home zipcode ({$zip}) ?</p>";}?>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?