Plug in PHP 100 POWER SOLUTIONS- P38 pptx

5 1.3K 0
Plug in PHP 100 POWER SOLUTIONS- P38 pptx

Đang tải... (xem toàn văn)

Thông tin tài liệu

C h a p t e r 7 : T h e I n t e r n e t 151 C h a p t e r 7 : T h e I n t e r n e t 151 do $str = substr(md5(rand(0, 1000000)), 0, $len); while (in_array($str, $shorts)); file_put_contents($file, $contents . $str . '|' . $url . "\n"); return $redirect . "?u=$str"; } Use Short URL Once you’ve created a short URL, you need a means to access it. You do this via a plug-in that accepts a short token in a GET tail, known as a query string, and then redirects the user to the equivalent longer URL. Figure 7-5 shows the function being used just to decode a short token, without redirecting. About the Plug-in This plug-in accepts a short token and returns its longer URL equivalent. It takes these arguments: • $token A short token with which to look up the equivalent URL • $file The datafile for this plug-in Variables, Arrays, and Functions $contents String variable containing the contents of $file $lines Array containing all the separate lines from $contents $shorts Array of short token versions of $longs $longs Array of full URL versions of $shorts $line String containing a single line extracted from $lines $j Integer counter for iterating through $longs FIGURE 7-5 Creating short URLs using your own domain is easy with this plug-in. 4 5 152 P l u g - i n P H P : 1 0 0 P o w e r S o l u t i o n s 152 P l u g - i n P H P : 1 0 0 P o w e r S o l u t i o n s How It Works This plug-in must be passed a short token as created by the previous plug-in, PIPHP_ CreateShortURL(). It then returns the associated URL. It does this by reading the contents of $file into the variable $contents, from where all the individual lines are extracted into the array $lines. Then two arrays ($shorts and $longs) are initialized to hold the short tokens and their long URL equivalents, as extracted from $lines. Then, if the data in $file was successfully loaded into $contents, an if statement is entered. In this case, a foreach loop iterates through all the lines in $lines and assigns the left and right items of data on either side of the | symbol, which divides them into the $shorts and $longs arrays. The function list() is called to extract both halves at once. Next the in_array() function is called to see whether $token already exists in the datafile. If so, the $shorts array, which contains the list of tokens, is stepped through, incrementing the pointer $j until the matching URL is found, at which point $j is used to index into $longs and extract the equivalent URL from there. This URL is then returned. If the token is not found in the datafile, FALSE is returned. How to Use It In the previous section I discussed a program called go.php. This is what we will write here. It’s very short and simple: $file = "shorturls.txt"; $result = PIPHP_UseShortURL($_GET['u'], $file); if ($result) header("Location: $result"); else echo "That short URL is unrecognized"; What this code does is fetch the argument passed to it in the GET variable 'u' and run it through PIPHP_UseShortURL(), which then looks up the associated URL and returns it to the string variable $result. The header() function is then called to issue a Location: header, informing the browser where the contents it is requesting can be found. All you need to do is save these four lines of code (along with the PIPHP_UseShortURL() plug-in, to your server’s document root as go.php (remembering to add the surrounding <?php and ?> tags), and it can be called up as follows (assuming your server has the domain name myserver.com): http://myserver.com/go.php?u=nnnn As long as nnnn is a valid short token, as created by plug-in 44, then this program will look up the associated URL and redirect the browser to it. But there’s a very neat trick you can employ to make this plug-in even more effective, and that’s to use mod rewrite to further modify the short URL, making it even shorter. You do this by creating (or editing) a file called .htaccess in the same directory as go.php. If you are using Windows, you will not be able to create the .htaccess file by right-clicking and selecting New because Windows will tell you that you need a file name before the period. Instead you must use a program editor to save the file, as most of these understand what a .htaccess file is, and can correctly create it. If you are using Windows Notepad or a program that doesn’t allow you to save an .htaccess file, just place double quotes around it (like this: ".htaccess") when saving, which tells Windows to save it as is. C h a p t e r 7 : T h e I n t e r n e t 153 C h a p t e r 7 : T h e I n t e r n e t 153 Once you have the .htaccess file, add the following two lines of code to it: RewriteEngine On RewriteRule ^([a-zA-Z0-9]+)$ go.php?u=$1 [L] What this does is tell the Apache web server that when it can’t find a file or folder on your server it should translate the filename requested (which can be any combination of letters and numbers) into the following form, where request is the original location requested: go.php?u=request So, for example, assume your web domain is myserver.com, you already have the short token 12345 which redirects to a valid URL, and you have entered the following short URL into your browser: http://myserver.com/12345 The mod rewrite module in the Apache web server will notice there is no file or folder named 12345 and therefore translate the request into the following: http://myserver.com/go.php?u=12345 And, hey presto, this is a valid URL pointing to the go.php program, which has been arrived at using the smallest possible short URL for your domain. If you find using mod rewrite and .htaccess don’t work for you, it may be because your httpd.conf configuration file doesn’t have AllowOverride enabled. In which case you’ll need to modify the relevant line and restart Apache. Under Windows, using Zend Server CE, you will find httpd.conf at c:\program files\zend\apache2\conf\httpd.conf. On Linux/Mac, you should find the file at /usr/local/zend/apache2/conf/httpd.conf. On other Apache installations the file may be elsewhere and you should consult the relevant documentation. You can open httpd.conf with any text editor and at (or somewhere near) line 211 you should see AllowOverride None, which should be changed to AllowOverride All. Then resave the file. If you are not allowed to save the file, you may need to adjust the file and/or folder permissions first. You should now restart Apache by clicking the Apache icon in your system tray and selecting Restart. Or, on Linux/Mac, using Zend Server CE, you would type /usr/local/ zend/bin/zendctl.sh restart into a Terminal window. For more about the mod rewrite program and .htaccess files on the Apache web server, please visit httpd.apache.org/docs/2.0/misc/rewriteguide.html. The Plug-in function PIPHP_UseShortURL($token, $file) { $contents = @file_get_contents($file); $lines = explode("\n", $contents); $shorts = array(); $longs = array(); 154 P l u g - i n P H P : 1 0 0 P o w e r S o l u t i o n s 154 P l u g - i n P H P : 1 0 0 P o w e r S o l u t i o n s if (strlen($contents)) foreach ($lines as $line) if (strlen($line)) list($shorts[], $longs[]) = explode('|', $line); if (in_array($token, $shorts)) for ($j = 0 ; $j < count($longs) ; ++$j) if ($shorts[$j] == $token) return $longs[$j]; return FALSE; } Simple Web Proxy There are times when you are unable to browse to a site from one location but you can ping it from a server at another location. You know the site should be up and running, but your connection to it from the original location is probably temporarily blocked. When this happens you can use this plug-in to act as a simple web proxy to browse right through to that site from your web server. Or, if you wish, you can use this code as a basis for your own web proxy service. You could provide this for free, or you could even drop in a small advertisement to cover bandwidth costs—although you’d have to add that code yourself. Figure 7-6 shows the www.news.com web site as browsed to using this plug-in. You can see from the status bar that all URLs in the page have been updated to call up linked pages through the proxy, too. And, yes, even the images have been served via the proxy. FIGURE 7-6 This small plug-in provides powerful web proxy functionality, including web images. 46 C h a p t e r 7 : T h e I n t e r n e t 155 C h a p t e r 7 : T h e I n t e r n e t 155 About the Plug-in This plug-in accepts a URL to fetch, and returns it with all URLs and links to images altered to run through the proxy. It takes these arguments: • $url The URL to fetch • $redirect The filename of a PHP program to act as the web proxy Variables, Arrays, and Functions $contents String containing the contents of $url $dom Document object of $contents $xpath XPath object for traversing $dom $hrefs Object containing all a href= link elements in $dom $sources Object containing all img src= link elements in $dom $iframes Object containing all iframe src= link elements in $dom $scripts Object containing all script src= link elements in $dom $css Object containing all link href= link elements in $dom $links Array of all the links discovered in $contents $to Array containing the version of what each $link should be changed to in order to ensure it is absolute $count Integer containing the number of elements in $to $link Each link in turn extracted from $links $j Integer counter for iterating through $to PIPHP_RelToAbsURL() Plug-in 21: This function converts a relative URL to absolute. How It Works This plug-in fetches the contents of $url and places it in $contents. If it cannot load the page at $url, FALSE is returned. Next, if $url refers to any image file such as .jpg, .gif, .png, or .ico, or any of a .css, .js, or .xml file, then the contents of the file are returned unaltered, as there is no need to attempt to convert relative links to absolute in these types of file because they are not HTML. However, any file that is not one of those mentioned here is assumed to be HTML. If you wish to improve on this plug-in, here’s one area where you could add support for many other file types. HTML will be assumed from here on, however. So the next thing that happens is all instances of &amp; (the XML and XHTML required form of the & symbol) are converted to just the & symbol, and then all & symbols are changed to a special token with the value !!**1**!!. You may wonder what on earth is going on here. Well, I can report that after a huge amount of time testing the str_replace() function built into PHP, I believe it has an obscure, and hard to catch, bug when it comes to processing the & symbol. In the end I gave up trying to find out why and simply chose to convert all occurrences of & to a sequence of characters I could be pretty sure would not appear in any HTML document, hence the string !!**1**!!. . $shorts $line String containing a single line extracted from $lines $j Integer counter for iterating through $longs FIGURE 7-5 Creating short URLs using your own domain is easy with this plug- in. . containing all img src= link elements in $dom $iframes Object containing all iframe src= link elements in $dom $scripts Object containing all script src= link elements in $dom $css Object containing. absolute $count Integer containing the number of elements in $to $link Each link in turn extracted from $links $j Integer counter for iterating through $to PIPHP_RelToAbsURL() Plug- in 21: This function

Ngày đăng: 07/07/2014, 08:20

Mục lục

  • Contents

  • Acknowledgments

  • Introduction

  • 1 Building a Development Server

    • Windows XP, Windows Vista, and Windows 7

      • Reinstalling Zend Server CE

      • Upgrading Zend Server CE

      • Windows Security Alerts

      • After Installation

      • Uninstalling

      • Document Root

      • Ubuntu and Debian Linux

        • Uninstalling

        • After Installation

        • Document Root

        • Fedora, RHEL, and CentOS Linux

          • Installing MySQL

          • Uninstalling

          • Document Root

          • Other Versions of Linux

            • Installing MySQL

            • Uninstalling

            • Document Root

            • Mac OS X 10.4 Plus on Intel Chips

              • Document Root

              • Uninstalling

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan