-
Processing page show. php:
- Include 'functions. php ';
- Include 'shorturls. php ';
-
- $ Entry = $ _ POST ['submit '];
- $ Url = $ _ POST ['URL'];
- $ Output = '';
-
- If ($ entry = 'Shorten url' AND $ Url)
- {
- If (! In_array ($ url, $ shortUrls ))
- {
- $ Uif = alphaID (count ($ shortUrls), false, 6 );
-
- If (! Array_key_exists ($ uif, $ shortUrls ))
- {
- $ ShortUrls [$ uif] = $ url;
-
- $ File = fopen ('shorturls. php', 'w ');
- Fwrite ($ file ,' ');
- Fclose ($ file );
- $ Output = 'original Url: '. $ url .'
Shorten Ulr: http://www.yourdomain.com/api/s?'.{uif .'';
- }
- Else $ output = 'Access error ';
- }
- Else
- {
- $ Uif = array_search ($ url, $ shortUrls );
- $ Output = 'original Url: '. $ url .'
Shorten Ulr: http://www.yourdomain.com/api/s?'.{uif .'';
- }
-
- Echo $ output .'
Back ';
- }
- ?>
Association page functions. php
- Function alphaID ($ in, $ to_num = false, $ pad_up = false, $ passKey = null)
- {
- $ Index = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ ";
- If ($ passKey! = Null ){
- // Although this function's purpose is to just make
- // ID short-and not so much secure,
- // With this patch by Simon Franz (http://blog.snaky.org /)
- // You can optionally supply a password to make it harder
- // To calculate the corresponding numeric ID
-
- For ($ n = 0; $ n
- $ Passhash = hash ('sha256 ', $ passKey );
- $ Passhash = (strlen ($ passhash) <strlen ($ index ))
- ? Hash ('sha512', $ passKey)
- : $ Passhash;
-
- For ($ n = 0; $ n <strlen ($ index); $ n ++) $ p [] = substr ($ passhash, $ n, 1 );
-
- Array_multisort ($ p, SORT_DESC, $ I );
- $ Index = implode ($ I );
- }
-
- $ Base = strlen ($ index );
-
- If ($ to_num ){
- // Digital number <-- alphabet letter code
- $ In = strrev ($ in );
- $ Out = 0;
- $ Len = strlen ($ in)-1;
- For ($ t = 0; $ t <= $ len; $ t ++ ){
- $ Bcpow = bcpow ($ base, $ len-$ t );
- $ Out = $ out + strpos ($ index, substr ($ in, $ t, 1) * $ bcpow;
- }
-
- If (is_numeric ($ pad_up )){
- $ Pad_up --;
- If ($ pad_up> 0) $ out-= pow ($ base, $ pad_up );
- }
- $ Out = sprintf ('% f', $ out );
- $ Out = substr ($ out, 0, strpos ($ out ,'.'));
- }
- Else
- {
- // Digital number --> alphabet letter code
- If (is_numeric ($ pad_up )){
- $ Pad_up --;
- If ($ pad_up> 0) $ in + = pow ($ base, $ pad_up );
- }
-
- $ Out = "";
- For ($ t = floor (log ($ in, $ base); $ t> = 0; $ t --){
- $ Bcp = bcpow ($ base, $ t );
- $ A = floor ($ in/$ bcp) % $ base;
- $ Out = $ out. substr ($ index, $ a, 1 );
- $ In = $ in-($ a * $ bcp );
- }
- $ Out = strrev ($ out); // reverse
- }
-
- Return $ out;
- }
- ?>
Link page shorturls. php
-
Principle: store the submitted URL in the array and generate a unique key for it. then, store the URL in the text. of course, you can also store the URL in the database, this instance is stored in text. Related functions 1. the array_search () function array_search (value, array, strict) array_search () function is the same as the in_array () function and finds a key value in the array. If this value is found, the key name of the matching element is returned. If not found, false is returned. 2. var_export () function this function returns the structure information about the variables passed to this function. it is similar to var_dump (). The difference is that the returned representation is a valid PHP code. You can set the second parameter of the function to TRUE to return the expression of the variable. Compare var_export () and var_dump (). Finally, accept the page redirect. php
- Include 'shorturls. php ';
- $ Uid = $ _ GET ['uid'];
- $ Addr = ($ uid AND array_key_exists ($ uid, $ shortUrls ))? $ ShortUrls [$ uid]: '/';
- Header ('Location: '. $ addr );
- ?>
This page must be used with. htaccess or. waccess:
- RewriteEngine On
- RewriteRule ^ s = (. *) $/redirect. php? Uid = $1 [R = 301, L]
|