Jump to content
Junitar

Page redirection if invalid URL parameter

Recommended Posts

Hello,

I've a photo gallery on my site that loads its content based on URL parameter (which is basically the name of the folder the images are taken from). I would like to make a redirection to the main galleries page if the parameter does not exist, is empty or wrong. I'm using an if statement with the header function but can't manage to get it work.

$galleryId is the id of the current gallery and $galleriesId is an array containing all the actual galleries id.

Here is the code:

$id = $_GET['id'];

if (isset($id) || !empty($id)) {

	//  Check if the current gallery id matches an actual gallery id
	if (!in_array($galleryId, $galleriesId)) {
		header("Location: ../galleries.php");
		exit;
	} else {
		// ...
	}
} else {
	header("Location: ../galleries.php");
	exit;
}

The correct URL has the following format: mysite.local/galleries/gallery.php?id=gallery-id

If I change the id value to, e.g. wrong-id, nothing happens. I just have the new URL mysite.local/galleries/gallery.php?id=wrong-id with every bit of HTML code before the exit function being executed.

Not sure if that makes a difference but I work on a virtual local host.

 

How can I do to redirect to the main galleries page when I have an invalid parameter?

Thanks

Edited by Junitar

Share this post


Link to post
Share on other sites

It won't redirect you mainly because header cannot be used like that.

Instead of using header just echo a javascript script:

echo '<script>window.location.href="url";</script>';

 

Share this post


Link to post
Share on other sites

This

!in_array($galleryId, $galleriesId)

does not makes sense? surly it should be

!in_array($id, $galleriesId)

and should this be

if (isset($id) && !empty($id))

To check if set and NOT empty.

NOTE: no html or spacing, line breaks of any kind should appear above this, for header to work.

 

 

 

Share this post


Link to post
Share on other sites

Simple example

gallery.php in galleries folder

<?php

$galleriesId = [];
$galleriesId[] = 1;
$galleriesId[] = 2;
$galleriesId[] = 3;
$galleriesId[] = 4;
$galleriesId[] = 6;
$galleriesId[] = 'gallery-id';
$id = $_GET['id'];

if (isset($id) && !empty($id)) {

    //  Check if the current gallery id matches an actual gallery id
    if (!in_array($id, $galleriesId)) {
        header("Location: ../galleries.php?gallery=Gallery%20Does%20Not%20Exist");
        exit;
    } else {
        header("Location: ../galleries.php?gallery=Gallery%20Exists");
        exit;
    }
}

galleries.php outside galleries folder

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <meta name="viewport" id="viewport" content="target-densitydpi=high-dpi,initial-scale=1.0,user-scalable=no" />
        <title>Document Title</title>
    </head>
    <body>
        <h1><?php echo $_GET['gallery']; ?></h1>
        <?php
        // put your code here
        ?>
    </body>
</html>

 

Share this post


Link to post
Share on other sites
1 hour ago, dsonesuk said:

This


!in_array($galleryId, $galleriesId)

does not makes sense? surly it should be


!in_array($id, $galleriesId)

 

Oops, my bad… Messed up the variable name I wrote in the post with the real variable name I used in my code. It's of course

!in_array($id, $galleriesId)

Though, I did make a mistake with the || and &&, thanks for pointing it out!

 

1 hour ago, dsonesuk said:

NOTE: no html or spacing, line breaks of any kind should appear above this, for header to work.

Ok, that was it. I had HTML above the PHP code. I've just made the changes to my code, placing the PHP part containing the header() before the doctype tag and it's now working. Spent so much time trying to figure out what was wrong… wasn't even aware of that. Well thank you very much for the help dsonesuk.

Thanks also Gabrielphp for your suggestion.

Edited by Junitar

Share this post


Link to post
Share on other sites

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.   Paste as plain text instead

  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.

Loading...

×
×
  • Create New...