doingword.com

Archive for the ‘tip’ Category

Get the name of the file using substr

Monday, December 10th, 2007

Hi guys,
Here the php code to get the actual name of the file. This is very usefull when you need to get the name of the file and you only have the full path like /download/games/getfile.php. To get the name I use the $_SERVER[”PHP_SELF”] var.
Here the code:

  1. $file = substr(strrchr($_SERVER["PHP_SELF"],"/"),1);

If $_SERVER[”PHP_SELF”] is /download/games/getfile.php the value of $file is getfile.php
Yes, I know, it’s very simple, but (at least for me) very usefull

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

Posted in Php, tip | No Comments »

How to get the extention of the file with php with strrchr function

Saturday, November 10th, 2007

Hi guys,
With this code you can get the extension of a file. This is very simple and useful.

  1. $ext = strrchr($file,".");

If the value of $file is “/files/games/mathattach.swf” the value of $ext will be “.swf”;

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

Posted in Php, tip | No Comments »

How to make a permanent reditect with php headers

Saturday, November 10th, 2007

Hi guys!
With this script you can make a permanent 302 redirection with php headers. This is very usefull when you want to make that google think that your domain.com is the same that www.domain.com.
Here the script:

if ($_SERVER[”HTTP_HOST”] == “www.domain.com”)
  1. {
  2. Header( "HTTP/1.1 301 Moved Permanently" );
  3. Header( "Location: http://domain.com".$_SERVER["REQUEST_URI"]);
  4. }

It’s very simple, but very usefull.

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

Posted in Php, SEO, tip | No Comments »

Protect files from direct download

Saturday, June 16th, 2007

Hi guys,
With this script you can control the access to your files with a ACL from secures referrers.
Here the code:

//Array with valid referers.
  1. $validos = Array
  2. (
  3. "http://www.domain.com/files.php",
  4. "http://www.domain.com/forum.php",
  5. "http://www.domain.com/download/file.php"
  6. };
  7.  
  8. //If is the referer in the array, we open the file
  9. if (in_array($_SERVER["HTTP_REFERER"],$validos))
  10. {
  11. //Open the file
  12. $file = $_SERVER["DOCUMENT_ROOT"]."/somefile.rar";
  13. //Send the header with the content type of the file
  14. header("Content-type: application/x-rar-compressed;");
  15. //Send the header with the length of the file
  16. header('Content-Length: ' . filesize($file));
  17. //Send the header with the name of the file
  18. header('Content-Disposition: attachment; filename= somefile.rar');
  19. //Put the content in the var
  20. readfile($file);
  21. }
  22. else
  23. //we redirect the user to another page.
  24. header("Location:http://www.domain.com/notpermision.php");
[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

Posted in Php, tip | No Comments »