FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


How to make a php page act differently





imagefree
By act differently i mean that i want a php page like:

http://www.example.com/dowloadfile.php?file=downloadable_file_name.pdf

How to do this?
(i am not asking about how to use the file variable. I have good knowledge of php but i dont know how to make the server send the file downloadable_file_name.pdf

also please tell me how to make a page like http://www.example.com/show.php?id=100 display an image so that <img src="http://www.example.com/show.php?id=100" /> displays an image.

another question similar to above two is how to make the page http://www.example.com/file.html send a downloadable file name.pdf to the browser.


Thanks you
(Please help me. I am in serious need of this info)
rvec
the first one can be done with header().

Just something like
Code:

<?php
$file= $_GET[file];
header('Location: {$file}');
?>

I'd recommend adding some security there like switch to make sure the user doesn't make his own headers.

For the image I think putting this: header("Content-type: image/png");
before outputting anything should do. But make sure the only output is an image and no text is included. If you want text in the image read this: http://www.php.net/gd (my sig is also made like that).

BTW if you want to use an image on a forum it has to end with one of the image extensions (.png .gif ...) you can do this with mod rewrite.

In the html file I think you need javascript.
http://www.tizag.com/javascriptT/javascriptredirect.php
that should do.
_AVG_
For the second one, you should first name the images by their ids and ensure they are all of the same extension. (eg: .jpg)

EG: 1.jpg, 2.jpg, etc.

Then, use this:

Code:
<?php
$id = $_GET[id];
$img = $id . ".jpg";
header('Location: {$id}');
?>
SamiTheBerber
$_REQUEST['variable_name'] will give you the variable from file.php?variable_name=value
rvec
SamiTheBerber wrote:
$_REQUEST['variable_name'] will give you the variable from file.php?variable_name=value

request will not only give the get variables but also the cookie and post variables. And I don't see how this post would help anyone Confused
imagefree
rvec wrote:

..........
I'd recommend adding some security there like switch to make sure the user doesn't make his own headers.
..........




can you please explain "if the user makes his own header, how will it be dangerous for my site/server?"
rvec
imagefree wrote:
rvec wrote:

..........
I'd recommend adding some security there like switch to make sure the user doesn't make his own headers.
..........




can you please explain "if the user makes his own header, how will it be dangerous for my site/server?"

I don't know. Can't think of anything. But I always try to keep the user input as minimal as possible just in case someone could exploit it.
kv
imagefree wrote:
rvec wrote:

..........
I'd recommend adding some security there like switch to make sure the user doesn't make his own headers.
..........


can you please explain "if the user makes his own header, how will it be dangerous for my site/server?"


Actually, depends on the header. User might inject some header which otherwise would not have been sent by the server normally.

Anyways, to make it secure, instead of using header, you can read the file and push it to browser in your code.

Code:


<?php

    $id = $_GET[id];
    $img = $id . ".jpg";
    $imgdir='/home/somedir/'; /*<-directory where your images reside. If it is inside webroot, you can use php variable to access the webroot directory. The advantage here is the file can be outside webroot, so it will be more secure. */

    readfile($imgdir.$img); //<- no header business here

?>

rvec
do you have an example of a header that might give problems or might even be a security risk?
imagefree
Code:

<?php
$file= $_GET[file];
header('Location: {$file}');
?>



does it automatically make a http link. I mean if $file="games.exe" , then the header should send the URL http://www.example.com/games.exe , is it?
rvec
Yeah it should work like that but kv just said a user can use that to do something you don't want him to do. I think he might be right but I don't know how.

Maybe use something like this instead:
Code:
$i = $_GET[file];
switch ($i) {
case "game.exe":
    header("Location:game.exe");
    break;
case "document.pdf":
    header("Location:document.pdf");
    break;
default:
    header("Location:document.pdf");
}

That will make sure the user gets redirected to one of the 2 files you want him to go to. You can of course add more cases.

BTW I used the filenames as links. This is not recommended but almost always works. If you want to use it on a site you should use the full url.
_AVG_
rvec wrote:
Yeah it should work like that but kv just said a user can use that to do something you don't want him to do. I think he might be right but I don't know how.

Maybe use something like this instead:
Code:
$i = $_GET[file];
switch ($i) {
case "game.exe":
    header("Location:game.exe");
    break;
case "document.pdf":
    header("Location:document.pdf");
    break;
default:
    header("Location:document.pdf");
}

That will make sure the user gets redirected to one of the 2 files you want him to go to. You can of course add more cases.

BTW I used the filenames as links. This is not recommended but almost always works. If you want to use it on a site you should use the full url.


I don't think that using a switch statement is a good idea .... I mean .... if there are many files then won't it be very taxing to enter about a 100 cases?
sonam
Quote:
I don't think that using a switch statement is a good idea .... I mean .... if there are many files then won't it be very taxing to enter about a 100 cases?

I am agree with this think. DB is more useful if you have large number of files.

Quote:
does it automatically make a http link. I mean if $file="games.exe" , then the header should send the URL


This is true but only if is your file is in root folder. Otherwise you will get 404 Error page. I am not sure am I good understand your questions (aaaahhhh, my English Embarassed ). Is this three questions in correlation (like you want with id=100, id=33, id=52 get different files on different location sometimes dowloadable, sometimes just show img, sometimes run exe) or it is separated.

Sonam
rvec
sonam wrote:
Quote:
I don't think that using a switch statement is a good idea .... I mean .... if there are many files then won't it be very taxing to enter about a 100 cases?

I am agree with this think. DB is more useful if you have large number of files.

Quote:
does it automatically make a http link. I mean if $file="games.exe" , then the header should send the URL


This is true but only if is your file is in root folder. Otherwise you will get 404 Error page. I am not sure am I good understand your questions (aaaahhhh, my English Embarassed ). Is this three questions in correlation (like you want with id=100, id=33, id=52 get different files on different location sometimes dowloadable, sometimes just show img, sometimes run exe) or it is separated.

Sonam

Yeah you could use something else instead of the switch but this was just an example. My point was you have to make sure every possibility is in your script and if none of those is chosen a default one is used.

the header doesn't only work in the root folder. But just like linking in an a href. You can use the full url but you can also use ../ or dir/index.html .
sonam
Quote:
Yeah you could use something else instead of the switch but this was just an example.


Sorry, you are right. I think how he need little bit from everything.

Quote:
the header doesn't only work in the root folder. But just like linking in an a href. You can use the full url but you can also use ../ or dir/index.html .


I know this, but maybe I didn't right understand his question: does it automatically make a http link.

If he write in url loctation eg. somefile.php?file="images/some.jpg" then he will automatically get file in images folder, if he write only file name eg. somefile.php?file="some.jpg" he will not get it, of course. First one example is not very secure for me, second one need aditional variable eg. $folder = "images/"; for right working. This is what I want to say.

Sonam
imagefree
by does it automatically make a http link. i mean if $file="file.pdf" , does placing this variable in header make the http link like http://www.example.com/file.pdf , or http://example.com/file.pdf ?
sonam
rvec wrote:
Quote:
But just like linking in an a href.


If user reach your site with www he/she will get www in url, if comming without www then url will not contain www.

Sonam
jerrylangu
You can do it in several ways in javascript ....

window.location = "websitename"

location.replace()

location.assign()

More info.....http://www.corelangs.com/js/basics/redirect.html


rvec wrote:
the first one can be done with header().

Just something like
Code:

<?php
$file= $_GET[file];
header('Location: {$file}');
?>

I'd recommend adding some security there like switch to make sure the user doesn't make his own headers.

For the image I think putting this: header("Content-type: image/png");
before outputting anything should do. But make sure the only output is an image and no text is included. If you want text in the image read this: http://www.php.net/gd (my sig is also made like that).

BTW if you want to use an image on a forum it has to end with one of the image extensions (.png .gif ...) you can do this with mod rewrite.

In the html file I think you need javascript.
http://www.tizag.com/javascriptT/javascriptredirect.php
that should do.
[url][/url]
creiglee
More about......Javascript Redirect

Lee
Related topics
E-Cards with PHP & MySQL
PB PHP, Iframe and Javascript
HTTP AUTH with PHP and mySQL
How To : Secure Your PHP Website
Never Mind Problem Resolved
PHP Page
Creating a page with php
help - download pop up on php page
how do i make a window/iframe in a table within php page..??
php page flickering, maybe caused by flash item
PHP page turning problem
PHP Whos Online Code OR Page refresh..
Can a php page generate a file?
PHP error page, die()
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.