Jump to content

Redirecting page to Dynamically Created Url


Go to solution Solved by AbraCadaver,

Recommended Posts

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 ";
?>
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 by AbraCadaver
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.

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.

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');

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. 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.