Jump to content

Need Some Help Creating Photo With PHP


Recommended Posts

Hello, I am making a photo with php, I wish to have a percentage of the people who view this photo to be redirected to a different site so I added this code to the php photo:

if (rand(0, 100) == "50"){
    header ("location: http://www.othersite.com/");
}

This works if you are accessing the photo directly from your browser, but if the photo is just included in an html file it doesn't work, is there a way that I could get this to work?

Link to comment
Share on other sites


You have to put that code in the begining (headers must be sent before ANY output to browser) of your html (actually php that generates your html) and add an exit call after sending header to prevent further output being sent to browser.

Link to comment
Share on other sites

You have to put that code in the begining (headers must be sent before ANY output to browser) of your html (actually php that generates your html) and add an exit call after sending header to prevent further output being sent to browser.

OK, so there is no code (i.e. JavaScript) that I could add to a php generated image that would make html pages which included the php photo be redirected?

Link to comment
Share on other sites

There is no simple solution to your problem. You generate php image probably with sth like that:

...
<img src="some_script.php" width="xx" height="yy" .../>
...

So code that is being executed in some_script.php can't affect page the image is displayed on, cause anything it generates browser takes as an image, just image.

But You can do some workaround for this... for example you could have to choose photo being displayed (and whether it should redirect or not) before displaying html.

A little pseudocode for that:

$image_nr = rand(...); // choose image to display, i don't know if your randomize it or what...

if ($image_nr should redirect && rand(0, 100) == 50) {
 header("Location: http://some.other.page.com");
 exit;
}

// else display html and image
// image number was chosen above, so we have to pass its ID to the script which generates an image somehow.. i.e.

// [header html...]
echo "<img src='generate_image.php?id=". $image_nr ."' alt='' />";
// [footer html...]

This will redirect random user instantly.. he won't even see that image, so if you want to add some delay to that, you would have to replace header() with some javascript code, ask if you want it that way, I can help.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...