14 Replies - 5179 Views - Last Post: 12 June 2011 - 01:44 PM Rate Topic: -----

#1 yaliteo  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 15
  • Joined: 30-May 11

PHP Redirect Not Working?

Posted 11 June 2011 - 01:24 AM

I am practicing coding in HTML and PHP and integrating them. I have an HTML page that looks like this:
<html>
<head>
<title>Hi</title>
</head>
<h2> Here you can download the picture</h2>
<p>Please Enter the Passcode Given to You Here</p>
<form name="input" action="password.php">
Password: <input type="password" name="password"/>
<p></p>
<input type="submit" value="Validate Password"/>
</form>
</html>


I named that submit.html and then created a password.php file and wrote the following code in that:
<?php
$_POST["password"];
$password=="yaliteo";
	if ($_POST["password"]==$password) {
	header("Location: /download.html");
	} else {
	header("Location: /submit.html");
	}

?>


After this I made a download.html. But my problem is that when I click on the Validate Password button in submit.html it does go to password.php but it never redirects.
Can anybody help me?
Much appreciated
Thanks

Is This A Good Question/Topic? 0
  • +

Replies To: PHP Redirect Not Working?

#2 dannymitza  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 23-October 10

Re: PHP Redirect Not Working?

Posted 11 June 2011 - 02:05 AM

You have to define a method for how form variables are processed. In your case, POST.

Change:
<form name="input" action="password.php">



To:
<form name="input" action="password.php" method="POST">


Was This Post Helpful? 0
  • +
  • -

#3 yaliteo  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 15
  • Joined: 30-May 11

Re: PHP Redirect Not Working?

Posted 11 June 2011 - 02:32 AM

Thanks for your help. It still won't redirect though.
I have changed the html to:
<html>
<head>
<title>Hi</title>
</head>
<h2> Here you can download the picture</h2>
<p>Please Enter the Passcode Given to You Here</p>
<form name="input" action="password.php" method="POST">
Password: <input type="password" name="password"/>
<p></p>
<input type="submit" value="Validate Password"/>
</form>
</html>


I thought it must then have something 2 do with the PHP.
Thanks again for your help
Was This Post Helpful? 0
  • +
  • -

#4 Ace26  Icon User is offline

  • D.I.C Head

Reputation: 40
  • View blog
  • Posts: 183
  • Joined: 10-August 08

Re: PHP Redirect Not Working?

Posted 11 June 2011 - 03:19 AM

View Postyaliteo, on 11 June 2011 - 10:24 AM, said:

<?php
$_POST["password"];
$password=="yaliteo";
	if ($_POST["password"]==$password) {
	header("Location: /download.html");
	} else {
	header("Location: /submit.html");
	}

?>



I think you should do away with the "/" in both header functions (on the assumption that both pages are located directly in the root directory of the server).

This post has been edited by Ace26: 11 June 2011 - 03:27 AM

Was This Post Helpful? 0
  • +
  • -

#5 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6058
  • View blog
  • Posts: 23,495
  • Joined: 23-August 08

Re: PHP Redirect Not Working?

Posted 11 June 2011 - 04:59 AM

$_POST["password"];

That line doesn't do anything.

$password=="yaliteo";

That's a comparison, not an assignment.

You should call exit() immediately after header to stop the processing of the current PHP script.
Was This Post Helpful? 1
  • +
  • -

#6 Ace26  Icon User is offline

  • D.I.C Head

Reputation: 40
  • View blog
  • Posts: 183
  • Joined: 10-August 08

Re: PHP Redirect Not Working?

Posted 11 June 2011 - 05:55 AM

View PostJackOfAllTrades, on 11 June 2011 - 01:59 PM, said:

$_POST["password"];

That line doesn't do anything.

$password=="yaliteo";

That's a comparison, not an assignment.

You should call exit() immediately after header to stop the processing of the current PHP script.


What! Can't believe I didn't see those! **slams head repeatedly on keyboard** But that should solve the problems Yaliteo was having. credits to monsieur JackOfAllTrades.
Was This Post Helpful? 1
  • +
  • -

#7 supercorey  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 119
  • View blog
  • Posts: 207
  • Joined: 15-February 09

Re: PHP Redirect Not Working?

Posted 11 June 2011 - 06:52 AM

One thing that I notice is in the headers. RFC 1945 requires an absolute URI such as
Location: http://www.example.com/download.html
. Most common browsers will support relative URIs such as
Location: /download.html
, but just something to keep in mind. That could possibly be an issue if all of the other fixes don't work.

This post has been edited by supercorey: 11 June 2011 - 06:53 AM

Was This Post Helpful? 2
  • +
  • -

#8 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6058
  • View blog
  • Posts: 23,495
  • Joined: 23-August 08

Re: PHP Redirect Not Working?

Posted 11 June 2011 - 07:27 AM

supercorey, that's an excellent point. I strive to be RFC-compliant if at all possible. Fortunately, like you say, most browsers don't seem to have a problem with it so I usually just "let it go". :)
Was This Post Helpful? 0
  • +
  • -

#9 yaliteo  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 15
  • Joined: 30-May 11

Re: PHP Redirect Not Working?

Posted 12 June 2011 - 03:43 AM

View PostAce26, on 11 June 2011 - 05:55 AM, said:

View PostJackOfAllTrades, on 11 June 2011 - 01:59 PM, said:

$_POST["password"];

That line doesn't do anything.

$password=="yaliteo";

That's a comparison, not an assignment.

You should call exit() immediately after header to stop the processing of the current PHP script.


What! Can't believe I didn't see those! **slams head repeatedly on keyboard** But that should solve the problems Yaliteo was having. credits to monsieur JackOfAllTrades.


I tried doing that, but it now says that there is a syntax error with my if, else statement.
<?php
	$_POST["password"];
	$password=="yaliteo";
	    if ($_POST["password"]==$password) {
	    header("Location: /download.html");
            exit();
	    } else {
	    header("Location: /submit.html");
	    exit(); }

	 
	?>


Why is this? It is always (no matter how much that I rearrange it) a syntax error on line 7. With the else statement.
Was This Post Helpful? 0
  • +
  • -

#10 Hiram  Icon User is offline

  • D.I.C Head

Reputation: 69
  • View blog
  • Posts: 203
  • Joined: 02-June 09

Re: PHP Redirect Not Working?

Posted 12 June 2011 - 04:46 AM

Well, on line 3 you're still doing the comparison, not assigning a value. Line 2 is also not doing anything, so try removing that one.

Try removing the exit() calls. If that's the complete script, exit() isn't necessary.

Edit: didn't see JackOfAllTrades recommendation about the exit calls :P

Still, give it a go and see how it goes.

This post has been edited by Hiram: 12 June 2011 - 04:48 AM

Was This Post Helpful? 0
  • +
  • -

#11 yaliteo  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 15
  • Joined: 30-May 11

Re: PHP Redirect Not Working?

Posted 12 June 2011 - 06:10 AM

Still not working!
I removed the exits and edited the code like this:
<?php
	$password="yaliteo";
	    if ($_POST["password"]==$password) {
	    header("Location: /download.html");
	    } else {
	    header("Location: /submit.html");
	    }
?>


But the } else { statement is still an issue and preventing me from running the code!

This post has been edited by yaliteo: 12 June 2011 - 06:11 AM

Was This Post Helpful? 0
  • +
  • -

#12 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6058
  • View blog
  • Posts: 23,495
  • Joined: 23-August 08

Re: PHP Redirect Not Working?

Posted 12 June 2011 - 06:44 AM

Quote

But the } else { statement is still an issue and preventing me from running the code!

What the hell does that MEAN???

It doesn't work
It still doesn't work
Was This Post Helpful? 0
  • +
  • -

#13 Ace26  Icon User is offline

  • D.I.C Head

Reputation: 40
  • View blog
  • Posts: 183
  • Joined: 10-August 08

Re: PHP Redirect Not Working?

Posted 12 June 2011 - 08:38 AM

I have made some corrections to your script and I think it will run just fine now. See below:
<?php
//Check if the form has been submitted else before loading for display by the browser, your 
//script will be executed and several errors will be generated. This way, that portion under
// the first "if" statement won't be executed until the form is submitted.
if(isset($_POST["validate"]))
{
 $password = "yaliteo";
    if ($_POST["password"]==$password) {
       header("Location: download.html");
       exit();
    } else {
     header("Location: submit.html");
     exit();
    }
}

?>



Also your html page was lacking some necessary components like the server communication method ("post" or "get") and submit button name. See modifications below:

<html>
<head>
<title>Hi</title>
</head>
<h2> Here you can download the picture</h2>
<p>Please Enter the Passcode Given to You Here</p>
<form name="input" method = "post" action="password.php">
<p>Password: <input type="password" name="password"/></p>

<input type="submit" name= "validate" value="Validate Password"/>
</form>
</html>



One suggestion though would be that you take out time to read(or re-read) about html forms.

Cheers.

This post has been edited by Ace26: 12 June 2011 - 09:00 AM

Was This Post Helpful? 2
  • +
  • -

#14 codeprada  Icon User is offline

  • Changed Man With Different Priorities
  • member icon

Reputation: 946
  • View blog
  • Posts: 2,355
  • Joined: 15-February 11

Re: PHP Redirect Not Working?

Posted 12 June 2011 - 11:38 AM

Don't check if $_POST['validate'] has been set but instead check $_POST['password'] since we'll need the value from that.
Was This Post Helpful? 1
  • +
  • -

#15 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3717
  • View blog
  • Posts: 5,981
  • Joined: 08-June 10

Re: PHP Redirect Not Working?

Posted 12 June 2011 - 01:44 PM

Two things worth checking when there are header issues.

1. That you didn't accidentally put stuff before the PHP block, like white-spaces (very common) or Unicode BOM chars (happens when using non-PHP friendly editors... like word-processors. (NEVER use word-processors!))

2. That you aren't surpressing any errors that might illuminate the situation.
// Only use this during development!
ini_set('display_errors', true);
error_reporting(E_ALL + E_NOTICE);

// ... SERIOUSLY!


This post has been edited by Atli: 12 June 2011 - 01:45 PM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1