...

View Full Version : Redirect not working properly



doubledee
08-24-2011, 04:13 AM
I am working on a module that allows Members to add comments to my articles. To do so, you must log-in or register.

I am testing the Article123 ---> Log In ---> Redirect to Article123 path...

My code was working last night except I was getting this annoying "Headers Already Sent" error message unless I added Output Buffering.

Late last night I moved most of my PHP before my HTML to eliminate this issue, but now my re-direct is broken?!

In my article.php I am setting the return page here...


// Set Article Title.
$articleTitle = $_GET['title'];
$_SESSION['articleTitle'] = $_GET['title'];

// Set current Script Name + Query String.
$_SESSION['returnToPage'] = $_SERVER['SCRIPT_NAME'] . '/' . $articleTitle;


When a User chooses to "Log In", the button that they click takes them to log_in2.php and this code is supposed to re-direct them back to the original article...

log_in2.php


// ****************************
// Check for Member Record. *
// ****************************
if (mysqli_stmt_num_rows($stmt)==1){
// Member was Found.

// Bind result variables.
mysqli_stmt_bind_result($stmt, $memberEmail, $memberFirstName);

// Fetch record.
mysqli_stmt_fetch($stmt);

$_SESSION['loggedIn'] = TRUE;
$_SESSION['memberEmail'] = $memberEmail;
$_SESSION['memberFirstName'] = $memberFirstName;

// Redirect User.
if (isset($_SESSION['returnToPage'])){
header("Location: " . $_SESSION['returnToPage']);
}else{
// Take user to Home Page.
header("Location: " . WEB_ROOT . "index.php");
}

// End script.
exit();
}else{
// Member not Found.
$_SESSION['loggedIn'] = FALSE;

$errors['pass'] = 'The E-mail and Password do not match those on file.';
}// End of CHECK FOR MEMBER RECORD.



I am also using the following mod_rewrite...


RewriteEngine on

#PRETTY: articles/postage-meters-can-save-you-money
#UGLY: article.php?title=postage-meters-can-save-you-money

RewriteRule articles/([a-zA-Z0-9_-]+)$ article.php?title=$1

# Build Date: 2011-08-22 7:08pm


As far as I can tell, when flow is re-directed back to "article.php", the GET variable is not getting set and this code is failing...

article.php


// Check for Title in URL.
if (isset($_GET['title'])){
// Title found in URL.

// Set Article Title.
$articleTitle = $_GET['title'];
$_SESSION['articleTitle'] = $_GET['title'];

// Set current Script Name + Query String.
$_SESSION['returnToPage'] = $_SERVER['SCRIPT_NAME'] . '/' . $articleTitle;

// Build query.
$q = 'SELECT html_title, meta_description, meta_keywords, page_title, page_subtitle,
written_on, author, body, reference_listing, endnote_listing
FROM article
WHERE article_title=?';

// Prepare statement.
$stmt = mysqli_prepare($dbc, $q);

// Bind variable.
mysqli_stmt_bind_param($stmt, 's', $articleTitle);

// Execute query.
mysqli_stmt_execute($stmt);

// Transfer result set from prepared statement.
// (Required for all queries that return results.)
mysqli_stmt_store_result($stmt);

// Check for Article Record.
if (mysqli_stmt_num_rows($stmt)==1){
// Article in Database.
$articleExists = TRUE;

// Bind result variables.
mysqli_stmt_bind_result($stmt, $htmlTitle, $metaDescription, $metaKeywords, $pageTitle, $pageSubtitle,
$writtenOn, $author, $body, $referenceListing, $endnoteListing);

// Fetch record.
mysqli_stmt_fetch($stmt);
}else{
// Article not in Database.
// Take user to Home Page.
header("Location: " . WEB_ROOT . "index.php");
}
}else{
// Title not found in URL.
// Take user to Home Page.
header("Location: " . WEB_ROOT . "index.php");
}// End of CHECK FOR ARTICLE IN URL.


And this path gets run...


}else{
// Title not found in URL.
// Take user to Home Page.
header("Location: " . WEB_ROOT . "index.php");
}// End of CHECK FOR ARTICLE IN URL.



Any ideas what my code re-write did to break things?! :(

Thanks,



Debbie

webdev1958
08-24-2011, 05:03 AM
header() (http://php.net/manual/en/function.header.php) will not work if any output has been created before the call to header() in your code.


Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP. It is a very common error to read code with include() (http://www.php.net/manual/en/function.include.php), or require() (http://www.php.net/manual/en/function.require.php), functions, or another file access function, and have spaces or empty lines that are output before header() is called. The same problem exists when using a single PHP/HTML file.

Have you got any output generated before you call header()?

doubledee
08-24-2011, 05:05 AM
header() (http://php.net/manual/en/function.header.php) will not work if any output has been created before the call to header() in your code.

Have you got any output generated before you call header()?

You missed *everything* I just said...


Debbie

webdev1958
08-24-2011, 05:08 AM
ok, if you say so :)

I'm sure you'll find where you have gone wrong eventually then ;)

djm0219
08-24-2011, 11:40 AM
As far as I can tell, when flow is re-directed back to "article.php", the GET variable is not getting set and this code is failing...

That is correct, there is no value to GET on your redirect. The GET value comes from this (?title=postage-meters-can-save-you-money) in the URL but what you are using after someone logs in is simply /postage-meters-can-save-you-money so there is no GET value to get.

Try changing your returnToPage session value to:


$_SESSION['returnToPage'] = $_SERVER['SCRIPT_NAME'] . '?title=' . $articleTitle;

and see what happens.

doubledee
08-24-2011, 09:53 PM
That is correct, there is no value to GET on your redirect. The GET value comes from this (?title=postage-meters-can-save-you-money) in the URL but what you are using after someone logs in is simply /postage-meters-can-save-you-money so there is no GET value to get.

Try changing your returnToPage session value to:


$_SESSION['returnToPage'] = $_SERVER['SCRIPT_NAME'] . '?title=' . $articleTitle;

and see what happens.

I don't whether to cry for joy because your one line of code fixed things or because it was so simple and I wasted so much time in vain last night?! :cool:

Thank You!!!!


Two follow-up questions...

1.) Why did this work when my PHP was in the middle of my HTML? (Vague, I know, but still...)


2.) Is my code acceptable in how I am handling things (including your fix)?

Apparently it is better that all of my logic/PHP is before the HTML display, and things do work okay so far, but did the code I posted look acceptable?! :confused:

Thanks,



Debbie

djm0219
08-25-2011, 01:36 PM
1.) Why did this work when my PHP was in the middle of my HTML? (Vague, I know, but still...)

Because the HTML had already been sent to the browser so your additional header command to redirect failed since headers had already been sent to the browser to output the HTML.

2.) Is my code acceptable in how I am handling things (including your fix)?

Your logic looks reasonable from here.

tangoforce
08-25-2011, 01:43 PM
Apparently it is better that all of my logic/PHP is before the HTML display, and things do work okay so far, but did the code I posted look acceptable?! :confused:


Apparently? - I badgered you several times about this and you've only NOW realised just WHY I was telling you this :p

Run logic first, generate any ouput required and then merge into HTML. Makes like a LOT easier for debugging Deb :thumbsup:

doubledee
08-25-2011, 06:47 PM
Apparently? - I badgered you several times about this and you've only NOW realised just WHY I was telling you this :p

What would I do without all of your badgering (I mean "wisdom"), Tango?! :p



Run logic first, generate any ouput required and then merge into HTML. Makes like a LOT easier for debugging Deb :thumbsup:

Yeah yeah yeah...


Debbie



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum