Php method for creating a short address (var_export)-php Tutorial

Source: Internet
Author: User
Php method for creating a short address (var_export)

Processing page show. php:

  1. Include 'functions. php ';
  2. Include 'shorturls. php ';
  3. $ Entry = $ _ POST ['submit '];
  4. $ Url = $ _ POST ['URL'];
  5. $ Output = '';
  6. If ($ entry = 'Shorten url' AND $ Url)
  7. {
  8. If (! In_array ($ url, $ shortUrls ))
  9. {
  10. $ Uif = alphaID (count ($ shortUrls), false, 6 );
  11. If (! Array_key_exists ($ uif, $ shortUrls ))
  12. {
  13. $ ShortUrls [$ uif] = $ url;
  14. $ File = fopen ('shorturls. php', 'w ');
  15. Fwrite ($ file ,' ');
  16. Fclose ($ file );
  17. $ Output = 'original Url: '. $ url .'
    Shorten Ulr: http://www.yourdomain.com/api/s?'.{uif .'';
  18. }
  19. Else $ output = 'Access error ';
  20. }
  21. Else
  22. {
  23. $ Uif = array_search ($ url, $ shortUrls );
  24. $ Output = 'original Url: '. $ url .'
    Shorten Ulr: http://www.yourdomain.com/api/s?'.{uif .'';
  25. }
  26. Echo $ output .'

    Back ';
  27. }
  28. ?>

Association page functions. php

  1. Function alphaID ($ in, $ to_num = false, $ pad_up = false, $ passKey = null)
  2. {
  3. $ Index = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ ";
  4. If ($ passKey! = Null ){
  5. // Although this function's purpose is to just make
  6. // ID short-and not so much secure,
  7. // With this patch by Simon Franz (http://blog.snaky.org /)
  8. // You can optionally supply a password to make it harder
  9. // To calculate the corresponding numeric ID
  10. For ($ n = 0; $ n
  11. $ Passhash = hash ('sha256 ', $ passKey );
  12. $ Passhash = (strlen ($ passhash) <strlen ($ index ))
  13. ? Hash ('sha512', $ passKey)
  14. : $ Passhash;
  15. For ($ n = 0; $ n <strlen ($ index); $ n ++) $ p [] = substr ($ passhash, $ n, 1 );
  16. Array_multisort ($ p, SORT_DESC, $ I );
  17. $ Index = implode ($ I );
  18. }
  19. $ Base = strlen ($ index );
  20. If ($ to_num ){
  21. // Digital number <-- alphabet letter code
  22. $ In = strrev ($ in );
  23. $ Out = 0;
  24. $ Len = strlen ($ in)-1;
  25. For ($ t = 0; $ t <= $ len; $ t ++ ){
  26. $ Bcpow = bcpow ($ base, $ len-$ t );
  27. $ Out = $ out + strpos ($ index, substr ($ in, $ t, 1) * $ bcpow;
  28. }
  29. If (is_numeric ($ pad_up )){
  30. $ Pad_up --;
  31. If ($ pad_up> 0) $ out-= pow ($ base, $ pad_up );
  32. }
  33. $ Out = sprintf ('% f', $ out );
  34. $ Out = substr ($ out, 0, strpos ($ out ,'.'));
  35. }
  36. Else
  37. {
  38. // Digital number --> alphabet letter code
  39. If (is_numeric ($ pad_up )){
  40. $ Pad_up --;
  41. If ($ pad_up> 0) $ in + = pow ($ base, $ pad_up );
  42. }
  43. $ Out = "";
  44. For ($ t = floor (log ($ in, $ base); $ t> = 0; $ t --){
  45. $ Bcp = bcpow ($ base, $ t );
  46. $ A = floor ($ in/$ bcp) % $ base;
  47. $ Out = $ out. substr ($ index, $ a, 1 );
  48. $ In = $ in-($ a * $ bcp );
  49. }
  50. $ Out = strrev ($ out); // reverse
  51. }
  52. Return $ out;
  53. }
  54. ?>

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

  1. Include 'shorturls. php ';
  2. $ Uid = $ _ GET ['uid'];
  3. $ Addr = ($ uid AND array_key_exists ($ uid, $ shortUrls ))? $ ShortUrls [$ uid]: '/';
  4. Header ('Location: '. $ addr );
  5. ?>

This page must be used with. htaccess or. waccess:

  1. RewriteEngine On
  2. RewriteRule ^ s = (. *) $/redirect. php? Uid = $1 [R = 301, L]

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.