kevkev
Newbie | Редактировать | Профиль | Сообщение | Цитировать | Сообщить модератору Здравствуйте! Не помню страничку, где взял это: 1 - generate_url.php <? /* * generate_url.php * * Script for generating URLs that can be accessed one single time. * */ /* Generate a unique token: */ $token = md5(uniqid(rand(),1)); /* This file is used for storing tokens. One token per line. */ $file = "/tmp/urls.txt"; if( !($fd = fopen($file,"a")) ) die("Could not open $file!"); if( !(flock($fd,LOCK_EX)) ) die("Could not aquire exclusive lock on $file!"); if( !(fwrite($fd,$token."\n")) ) die("Could not write to $file!"); if( !(flock($fd,LOCK_UN)) ) die("Could not release lock on $file!"); if( !(fclose($fd)) ) die("Could not close file pointer for $file!"); /* Parse out the current working directory for this script. */ $cwd = substr($_SERVER['PHP_SELF'],0,strrpos($_SERVER['PHP_SELF'],"/")); /* Report the one-time URL to the user: */ print "Use this URL to download the secret file:<br><br>\n"; print "<a href='http://".$_SERVER['HTTP_HOST']. "$cwd/get_file.php?q=$token'>\n"; print "http://".$_SERVER['HTTP_HOST']."/!!!/get_file.php?q=$token</a>\n"; ?> 2 - get_file.php <? /* * get_file.php * * Script for validating a request through a secret token, passing a file * to the user, and ensuring the token can not be used again. * */ /* Retrive the given token: */ $token = $_GET['q']; if( strlen($token)<32 ) { die("Invalid token!"); } /* Define the secret file: */ $secretfile = "/tmp/secret_file.txt"; /* This variable is used to determine if the token is valid or not: */ $valid = 0; /* Define what file holds the ids. */ $file = "/tmp/urls.txt"; /* Read the whole token-file into the variable $lines: */ $lines = file($file); /* Truncate the token-file, and open it for writing: */ if( !($fd = fopen($file,"w")) ) die("Could not open $file for writing!"); /* Aquire exclusive lock on $file. */ if( !(flock($fd,LOCK_EX)) ) die("Could not equire exclusive lock on $file!"); /* Loop through all tokens in the token-file: */ for( $i = 0; $lines[$i]; $i++ ) { /* Is the current token the same as the one defined in $token? */ if( $token == rtrim($lines[$i]) ) { $valid = 1; } /* The code below will only get executed if $token does NOT match the current token in the token file. The result of this will be that a valid token will not be written to the token file, and will therefore only be valid once. */ else { fwrite($fd,$lines[$i]); } } /* We're done writing to $file, so it's safe release the lock. */ if( !(flock($fd,LOCK_UN)) ) die("Could not release lock on $file!"); /* Save and close the token file: */ if( !(fclose($fd)) ) die("Could not close file pointer for $file!"); /* If there was a valid token in $token, output the secret file: */ if( $valid ) { readfile($secretfile,"t"); } else { print "Invalid URL!"; } ?> Подскажите, как его использовать??? |