Jump to content
Larry Ullman's Book Forums

Reidrect To A New Php Page


Recommended Posts

Hello,

 

Following the guidance in chpt. 8 of this great book, I am unable to get the header () function to redirect to the welcome.php page ('Location: welcome.php');.

 

I've tried every possible combination of paths to the welcome.php page, and all I get when clicking the login button is a blank page (still titled "login"). I've tried it on my development platform with the latest versions of Apache, and PHP. I even tried it on my host (bluehost.com). Lastly, out of frustration, I cut and pasted every one of the examples from the downloaded scripts (in case I made an error in reproducing what I see in the book), all to no avail. Every other feature that chapter 8 calls for works perfectly. The login page, with the redirect is shown below: (see the code about half-way down the page).

 

Thanks for your help!

 

 

<?php // Script 8.13 - login.php #2

/* This page lets people log into the site (in theory). */

// Set the page title and include the header file:

define('TITLE', 'Login');

include('templates/header.html');

// Print some introductory text:

print '<h2>Login Form</h2>

<p>Users who are logged in can take advantage of certain features like this, that, and the other thing.</p>';

// Check if the form has been submitted:

if ($_SERVER['REQUEST_METHOD'] == 'POST') {

// Handle the form:

if ( (!empty($_POST['email'])) && (!empty($_POST['password'])) ) {

if ( (strtolower($_POST['email']) == 'me@example.com') && ($_POST['password'] == 'testpass') ) { // Correct!

 

// Redirect the user to the welcome page!

ob_end_clean(); // Destroy the buffer!

header ('Location: welcome.php');

exit();

 

} else { // Incorrect!

print '<p>The submitted email address and password do not match those on file!<br />Go back and try again.</p>';

 

}

} else { // Forgot a field.

print '<p>Please make sure you enter both an email address and a password!<br />Go back and try again.</p>';

 

}

} else { // Display the form.

print '<form action="login.php" method="post">

<p>Email Address: <input type="text" name="email" size="20" /></p>

<p>Password: <input type="password" name="password" size="20" /></p>

<p><input type="submit" name="submit" value="Log In!" /></p>

</form>';

}

include('templates/footer.html'); // Need the footer.

?>

Link to comment
Share on other sites

There are only three things I can think of:

1) Your path to welcome.php is wrong.

2) The ob_end_clean() function is causing problems because you never call the ob_start() function.

3) There is a server config issue (which seems unlikely, given all the platforms you've tested your script on).

 

Good luck.

  • Upvote 1
Link to comment
Share on other sites

Thank you HartleySan,

 

I have tried every possible combination of path to welcome.php. Welcome.php is in the same directory as login.php. (however, I still tried every possible path). The ob_start() function is called in an include file (per the instructions in the book I am following). And, as you have pointed out, the server config is not an issue since I've tested it many times. (I even loaded it to my host server and the same problem exists).

 

I'm certain it is a small error, but I cannot find it after 5 hours of work.

 

Thanks for your reply.

Link to comment
Share on other sites

When I hit this kind of brick wall (and it happens often), I always try to boil my code down to the lowest common denominator.

For example, I think the first thing we need to establish is whether it's an issue with the header location function or not (as I am thinking it might be something unrelated causing the problem).

 

I would recommend making two sample scripts like the following:

 

index.php

<?php

 header('Location: index2.php');

 echo 'This is index.php.';

?>

 

index2.php

<?php

 echo 'This is index2.php.';

?>

 

Then, place both scripts in the same folder on your server or local server, and run index.php. See which string is displayed (and if any errors are displayed). If the index2.php text is displayed, then you know that the header location function call is not the problem. From there, I'd start adding your original script back, bit by bit, until you identify the error.

 

Good luck.

 

Edit: I slightly edited the first script, as you really should call the header function before anything is echoed or output to the screen. Sorry for that.

  • Upvote 1
Link to comment
Share on other sites

HartleySan,

 

I tried this (using index1.php and index2.php). The echo came back as "This is index1.php." (this is the file I had the header() on). The problem still exists. I tried it also on another site I maintain (different directory) and it also did not work. Finally, I loaded the 2 files to the production server (my host provider) and it did not work there either.

 

I've never been so confused!

 

Thanks for trying

Link to comment
Share on other sites

Firstly, have you set display_errors and error_reporting? I suspect you haven't and when you do you'll get a Headers already sent message.

 

Secondly, check for and remove whitespace in your script before the header call.

  • Upvote 2
Link to comment
Share on other sites

Thanks for your response Rob.

 

I have checked and ensured (php.ini) that the error reporting was on. I've checked and rechecked for any whitespace before the header call. The script I'm using was copied (cut and pasted) directly from the author's online script.

 

As frustrating as this is, there does not seem to be any apparent problem (although I know there is something causing the problem).

 

Thanks

Link to comment
Share on other sites

It has to be something with you environment setup. It sounds crazy, considering all the different environments/servers you've tested on, but yeah.

 

Could you please tell us the exact environment you're working in? Also, maybe try executing other header functions, and see whether they execute properly or not. Maybe the header function is being blocked for one reason or other.

  • Upvote 1
Link to comment
Share on other sites

Hello HartleySan,

 

The environment is: Apache 2.2, PHP 5.2, MS Expression Development Server.

 

I've tried in IE9, FF 15.0.1, Chrome 22.0xxxx.

 

I tried sending a function header('WWW-Authenticate: Basic realm="Test Authentication System"'); It worked okay.

 

 

The problem is only with the redirect. I also ensured that the BOM for UTF-8 was turned off (to prevent any hex characters being sent ahead of the header). Unfortunately, I've decided to use some javascript code for the redirect, this seems to work satisfactorily. One day, if I ever determine the problem with the PHP header redirect, I'll post on this forum.

 

Thanks again for all of the time you have taken on my behalf,

 

Bob

Link to comment
Share on other sites

Yeah, sorry I couldn't be of more assistance.

What a bugger though!

 

It's amazing that scripts that works perfectly fine for me are throwing all sorts of loops for you, especially for just one type of header function call and nothing else.

 

This really is a mystery.

As a last resort, I'd recommend maybe trying a different text editor for your code, and instead of copying and pasting the code, write a simple redirect script from scratch in that text editor and see what happens.

 

Anyway, if you are tired of dealing with this and just want to move on, I totally understand.

It's bothering me too though. I hope you find a solution (for both of our sakes).

Link to comment
Share on other sites

Thanks Rob, I appreciate the information. I did google extensively and did not find a cure for the issue. However, I've just downloaded Fiddler and will attempt to isolate the problem. I'm certain there is a solution, but it will take more work to resolve.

Link to comment
Share on other sites

  • 2 years later...

Hello all (and hopefully Hartley Sans, my dear online friend, too)! 

 

Please kindly note that I want to do an automatic PHP (I believe 301) redirect to an amazon site as an affiliate.  Is it still possible to use meta description in a page that automatically gets redirected to Amazon.

 

I've never actually uploaded a PHP page.  Would I just make a folder with the PHP file in it with the redirect and attach it to the HTML page that gets redirected?  Do I have to do anything special at the server end?

 

Also, what would be the code for a simple 301 redirect in PHP?  Also, should I use a JavaScript and an HTML redirect along with the PHP redirect, or are they less SEO friendly?

 

So much to ask!  Any assistance is a God sent!!  Thank you so very much for any and all insights!

Link to comment
Share on other sites

  • 5 months later...

Have you got this problem fixed yet?

 

Did you try the full url?  header ("Location: http://www.domainname.ext");as mentioned on page 229 in the tip?

 

Also did you put <?php

ob_start();

?>  at the very top of the page before <!doctype  etc.  ?

 

I'm having a similar problem. 

 

The form is part way down the page and not at the top of the page and I want it to go that specific location on the SAME page and not a different page, where the messages appear.  I had problems using header ("Location:  ):.  It either went to the location on the page I wanted without the messages or saw the messages and at the top of the page but not both as I wanted.  Annoying.  On www.php.net I found a suggestion to use header ("refresh:5; url=http://www.domainname.ext#messages");and that worked on firefox, IE and safari.  However a problem with chrome and opera.  It goes to the top of the page, does not show the messages and does not show any info. entered in the form fields (sticky forms).  I don't know what else to try. 

Any suggestions from anyone???

 

Hopefully you get it working.

Link to comment
Share on other sites

The #messages is just an anchor reference and is the right solution to redirecting someone to a specific point in a page. If it's not working on Chrome and Opera, you probably haven't properly defined the anchor, by creating an element with an "id" property that has a value of "messages". 

Link to comment
Share on other sites

 Share

×
×
  • Create New...