Forum Moderators: coopster

Message Too Old, No Replies

PHP variable redirect help

I need some help with a simple variable redirect

         

Akatosh24

4:48 am on Aug 30, 2011 (gmt 0)

10+ Year Member



Well, today I decided "Hey, I'm gonna make a website! Haven't done that in a while!". Unfortunately, I have forgotten almost all of my PHP knowledge, so I hit a bit of a speed bump pretty early on. The page is only meant for a few people, or people who have proven themselves savvy enough to get in. So I made a cipher holding the password to the page. I gave the input box for the password a name, linked the HTML form to an external PHP file... and was immediately stopped by the fact that I don't remember how to do this. I basically just need something that says "if $p is "password" then go to this page, else go to a different page. Any help would be GREATLY appreciated. Thanks!

-Nick

omoutop

6:53 am on Aug 30, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



you need to take a look at the header() function.

A basic script of what you want may be something like:
if ($var=='a')
{
header("Location: pageA.htm");
}
else
{
header("Location: pageB.htm");
}

Akatosh24

4:04 pm on Aug 30, 2011 (gmt 0)

10+ Year Member



Thanks! Works perfectly.

Akatosh24

4:10 pm on Aug 30, 2011 (gmt 0)

10+ Year Member



Wait, I was wrong. It works almost perfectly. Even if the password is correct it redirects to index.html as if it was incorrect. Here's my code.

<?php
$p = $_REQUEST['p'];
$sl = 'sitelink1';
$fmxb = 'sitelink2';

if ($p=='password')
{
header("Location: $fmxb");
}
else
{
header("Location: $sl");
}

?>

g1smd

4:41 pm on Aug 30, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



When using HEADER, the URL must also be complete with protocol and domain name.

Using a Location header on its own serves a 302 redirect. Is that what you want?

Shouldn't you use an "include" instead of a redirect here?

Akatosh24

4:48 pm on Aug 30, 2011 (gmt 0)

10+ Year Member



No, an include would simply place another PHP file into the one here. I want to have the browser redirect to another HTML page.

Akatosh24

1:24 am on Aug 31, 2011 (gmt 0)

10+ Year Member



Never mind! Turns out I just had the password typed wrong in the PHP file, so that code works PERFECTLY. Thanks to omoutop for telling me! I can do the forums now too, cuz I can use a modified version of that for a login page.

Readie

11:43 am on Aug 31, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



When using HEADER, the URL must also be complete with protocol and domain name.

I'm not sure if protocol & domain are implied or not with a leading forward slash, but header('Location: /some/url'); works fine.

g1smd

11:49 am on Aug 31, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



No. The specs require a fully qualified location to be specified.

It might appear to "work" in some browsers and in some situations, but you should not rely on that.

Be aware that you have a 302 redirect. You'll need another line to specify 301.