Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

redirecting in server works with OnLoad but not with header()

Status
Not open for further replies.

alico1233

Programmer
Jan 12, 2005
5
TR
Hello;
I am using php for passwording my pdf files.But if you write the exact location of pdf, it is opened(naturally) without questioning password. Therefore I use redirecting pdf file from server to the php page.And it works if php opens yhe file with:

?><BODY onLoad="location.replace('../directory/test.pdf')"></BODY><?

However, I want to use in php POST method,namely taken the information from address bar.In order to manage this, I am adviced in this forum:

header("Location: ['HTTP_HOST']."/".$_GET['name']);

It works in php.But this time redirecting does not works.

As a result,is there a way to use post(or get) method in order to create only one php page for hundreds pdf's AND to use redirecting?
 
If the "redirecting" isn't working, then chances are you have something that's outputting before your header() is called. What is in your code above the header()?

Also, if you're taking a variable directly from the address bar (in a query string), that's a GET, not a POST.

If you want to verify that the person is authenticated before showing the pdf file, then you should be able to do that before the header() call as long as nothing is output. You could do something like the following:

Code:
<?php
  if ($authenticated == 1)
  {
    header("Location: [URL unfurl="true"]http://".$_SERVER[/URL]['HTTP_HOST']."/".$_GET['name']);
  }
  else
  {
    echo("You have to log in before you can view this document.");
    include("page_to_do_authentication.php");
  }
?>
I think that's what you were asking... but maybe if you could provide some more code details we can have a look.

Marc
 
Hello Marc,
my problem: (Am I right)If I only use 'code', you can allready open test.pdf writing in explorer (if you know the exact location).I want to prevent this. And for this purpose I should make from server "something", because I can't write code in pdf-file (am I right).

So I think that I can use redirecting preference in server and redirect all pdf-files to php-login-page. At the same time I want to use GET method in order not to create a php page for every pdf-file, and use only one php-login-page for every pdf-file.

Now: I was using "onload" to open (open means here only shoving existing pdfs) pdf-files AND redirecting was working: So you can not open a pdf file unless writing username/password even if you know the exact loaction of it.

Then: (With your very advice) I changed the line for opening pdf-file from "onload" to "header()" because "onload" did not worked with GET. It worked for using single php with GET method.BUT,THIS TIME redirecting doesn't work. It is redirecting to php and asks u/p with form but when you write right words, it tries a relatively very long time and died.No error message.

the code:

<?php

if (isset($HTTP_COOKIE_VARS["kullaniciadi"]))
{
header("Location: ['HTTP_HOST']."/".$_GET['name']);
}


if (($gonderim) && ($_GET['name'] <> ""))
{

if ($kullaniciadi=='username')
{
if ($sifre=='password')
{
SetCookie("kullaniciadi",$kullaniciadi,time()+60);
header("Location: ['HTTP_HOST']."/".$_GET['name']);
}
else

{ echo("Yanlis kullanici adi veya sifre. bi daha:\n");

?>
<form action="<?php echo $_SERVER['PHP_SELF']."?name=".$_GET['name']; ?>" method=post>
<input type="text" name="kullaniciadi" size="6" maxlenght="16">
<input type="password" name="sifre" size="6" maxlenght="16">
<input type="submit" name="gonderim" value="Gönder">
</form>
<?
exit;
?>

This works for GETting from address bar,but not redirecting.

The older was different in form action and in header() line: that was something like this:

onload(LOcation.replace:"../directory/test.pdf")<body></http>

:)I know this is very long but I wanted to explain exactly my problem:

Shortly:With "anyway" I want to let pdfs reachable ONLY using password.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top