Jump to content

PHP redirect script [solved]


Eeeeediot

Recommended Posts

Well I'm trying to get a PHP redirect script to work on a website, but its not very straight forward, and not being an expert in PHP I need some help. I'll try to explain by situation as best as I can:

 

 

 

Basically the home page of my website (index.php) is including another page which contains the latest 2 news articles from a basic content management system (CMS).

 

 

 

To add a piece of news I write the headline and the article in the CMS and click 'Post'. The first few words of the article are displayed on the home page with a link to the page displaying the rest of the article (article.php?id='article_number').

 

 

 

Here's my problem. I don't want the link to go to the page displaying the rest of the article (article.php?id='article_number'), I want it to go to a different page with a form to enter a competition (form.php) as this is what the news item is about. There is no way to change the link so I'm trying to redirect the user from the (article.php?id='article_number') page to the (form.php)

 

 

 

I know that I cannot just add a simple PHP redirect script (like that below) into the place where I normally add the article:

 

 

 

<?php header( 'Location: form.php' ) ; ?>

 

 

 

as such a script needs to be placed above the tag of a page to work. I figure that the only way to make this work is to add the PHP redirect script above the tag of the (article.php) file itself, and only redirect the user if the URL is (article.php?id=25) as other news items (that use article.php) which I want to work normally will be redirected to (form.php) if no condition is placed, which I don't want.

 

 

 

Not being very good at PHP I've come up with a novice attempt at an IF function that so far hasn't worked:

 

 

 

<?php

if ("Location = 'article.php?id=25'") {

      header("Location: 'form.php'");

      } else {







rest of the code in article.php...







      }

?>

 

 

 

So I would appreciate any help from anyone who can correct my PHP redirect script with the IF function, or anyone that can suggest another way to solve my problem, through PHP, Javascript or anything else. Also please tell me if the explanation of my problem is not clear.

 

 

 

Many thanks in advance.

Link to comment
Share on other sites

<?php 

  if ($_GET['id'] == 25) { 

      header("Location: form.php"); 

      } else { ?>







rest of the code in article.php... 







<?php       } 

?>

 

Right now, your if statement is just checking for a string which will return true. Also, you need to close the php tag, otherwise the interpreter will treat your HTML as php code.

 

 

 

Edit: Oh, and I don't think you need the quotes around 'form.php'

[sig too big, don't do it again]

Link to comment
Share on other sites

It'd be better to write it is

 

 

 

<?php

if (@$_GET['id'] == 25)

{

  header("Location: form.php");

}

else

{

?>

html stuffs

<?php

}

?>

 

 

 

Otherwise you're going to get a die error for an undefined index if is isn't a GET variable when the script is called.

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
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.