Bigdiogi Posted October 11, 2013 Share Posted October 11, 2013 Totally ignorant about PHP. Have found a script to create a one time URL. Works great. But is there an EASY way to redirect to the url created instead of having it print. In other words, can I change the print: command with one to immediately redirect the page. Kepp my ignorance in mind. And be gentle, please. Last lines of the script look like - print "Use this URL to download your coupon:<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 "; ?> Quote Link to comment Share on other sites More sharing options...
AbraCadaver Posted October 11, 2013 Share Posted October 11, 2013 (edited) header("Location: http://".$_SERVER['HTTP_HOST']."/get_file.php?q=$token"); exit; If $token has any special characters in it then you will have to do: header("Location: http://".$_SERVER['HTTP_HOST']."/get_file.php?q=".urlencode($token)); exit; Edited October 11, 2013 by AbraCadaver Quote Link to comment Share on other sites More sharing options...
Bigdiogi Posted October 11, 2013 Author Share Posted October 11, 2013 header("Location: http://".$_SERVER['HTTP_HOST']."/get_file.php?q=$token"); exit; If $token has any special characters in it then you will have to do: header("Location: http://".$_SERVER['HTTP_HOST']."/get_file.php?q=".urlencode($token)); exit; But from all the research I've, which granted - much of it went right over my head, I thought the header info had to be used before any of the rest of the page executed? The URL creation is the las line of script, after all the rest of the various checks, etc. are performed. Quote Link to comment Share on other sites More sharing options...
Bigdiogi Posted October 11, 2013 Author Share Posted October 11, 2013 Studied your response more carefully. See what you have done now. The header calls. get file.php. Was looking for a replcement for the print command. We'll try it. Quote Link to comment Share on other sites More sharing options...
Bigdiogi Posted October 11, 2013 Author Share Posted October 11, 2013 I couldn't get that to work at through my vast and overwhelming ignorance. Maybe I didn't give enough information. Have generate_url,php in a page. It reads like this <? /* * 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 = "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 your coupon:<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 "; ?> It calls 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 = "dealers_choice.html"; /* This variable is used to determine if the token is valid or not: */ $valid = 0; /* Define what file holds the ids. */ $file = "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("urls.txt","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); } else { print "Invalid URL!"; } ?> Which prints to a new page. Trying to skip the clickable URL part, and just go to the generated link. Quote Link to comment Share on other sites More sharing options...
Barand Posted October 11, 2013 Share Posted October 11, 2013 :code_tags: Quote Link to comment Share on other sites More sharing options...
AbraCadaver Posted October 11, 2013 Share Posted October 11, 2013 So delete all the print lines and add the header line there. Quote Link to comment Share on other sites More sharing options...
Bigdiogi Posted October 11, 2013 Author Share Posted October 11, 2013 Already tried that. No joy. Quote Link to comment Share on other sites More sharing options...
AbraCadaver Posted October 11, 2013 Share Posted October 11, 2013 Already tried that. No joy. No joy what? It doesn't redirect or you don't get the file or what? Previously did the clickable link work? Add this to the top of generate_url.php after the <? and run it again. error_reporting(E_ALL); ini_set('display_errors', '1'); Quote Link to comment Share on other sites More sharing options...
Bigdiogi Posted October 11, 2013 Author Share Posted October 11, 2013 Yes, the clickable link worked. When I replaced all the print statements with either header("Location: http://".$_SERVER['HTTP_HOST']."/get_file.php?q=$token");exit; or header("Location: http://".$_SERVER['HTTP_HOST']."/get_file.php?q=".urlencode($token)); exit; got an url in the address bar server/host/get_file.php?q=the_generated_token_number but get_file has a declared file as a variable. Somehow, it's not included in the URL. Get a file not found message Inserted the error reporting code. Gave me no more information than before. These were the same errors I've been getting. Quote Link to comment Share on other sites More sharing options...
Solution AbraCadaver Posted October 11, 2013 Solution Share Posted October 11, 2013 OK, there were two different URLs in the print statements and I got the wrong one: header("Location: http://".$_SERVER['HTTP_HOST']."$cwd/get_file.php?q=$token"); Quote Link to comment Share on other sites More sharing options...
Bigdiogi Posted October 11, 2013 Author Share Posted October 11, 2013 Bingo! That dood it! Thanks so very much for all your help and patience. It is greatly appreciated. Bruce Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.