Jump to content

Clarification on Headers Already Sent


doubledee

Recommended Posts

I think I found out where my issue is at, but could use some "expert" help - especially since I can't post everything here...

 

Once again, what I am doing is creating an "Add a Comment to the Article" module.

 

The User is on some article, and wants to comment on the article, but they need to be logged in or register.

 

In this scenario, the User is a Member and chooses to Log-In.

 

Below is the part of the code where they successfully log in...

 

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.

 

 

Now the User is *supposed to be* re-directed back to the article they were originally on...

 

article.php

<?php
// Initialize a session.
session_start();

/*
foreach($_SESSION as $sessvar) {
	unset($sessvar);
}
*/

// Initialize Logged-In Status.
//	$_SESSION['loggedIn'] = TRUE;
$_SESSION['loggedIn'] = FALSE;

// Access Constants.
require_once('config/config.inc.php');

// Connect to the database.
require_once(ROOT . 'private/mysqli_connect.php');

// Initialize variables.
$articleExists = FALSE;
$id = $articleTitle = '';
$htmlTitle = $metaDescription = $metaKeywords = $pageTitle = $pageSubtitle = '';
$writtenOn = $author = $body = $referenceListing = $endnoteListing = '';

// 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.
?>

 

The problem is that everytime I log-in to my test user, they get re-directed to index.php which isn't right...

 

(I had this working properly when the code above was nested in my HTML and I used Output Buffering.  Now the "Header Already Sent" problem is fixed and I have this incorrect re-directing issue?!)  >:(

 

Where things seem to break down is the hand-off from "log_in2.php" back to "article.php"...

 

Any ideas what is going on?

 

 

Debbie

 

 

Link to comment
Share on other sites


function redirect($pURL) {
if (strlen($pURL) > 0) {
if (headers_sent()) {
echo "document.location.href='".$pURL."';\n"; }
else {
header("Location: " . $pURL);
}
exit();
}
}

example.php


redirect("''".WEB_ROOT."'/index.php");

 

then you don't have to worry about using output buffering as a "band aid" fix  8)

Link to comment
Share on other sites

Do you have session_start() at the top of log_in2.php?  Are you sure that the title is set properly when you redirect from your login script to your article?  Have you tried commenting out one of the header redirects in order to see which one is actually bringing you back to the index?

Link to comment
Share on other sites


function redirect($pURL) {
if (strlen($pURL) > 0) {
if (headers_sent()) {
echo "document.location.href='".$pURL."';\n"; }
else {
header("Location: " . $pURL);
}
exit();
}
}

example.php


redirect("''".WEB_ROOT."'/index.php");

 

then you don't have to worry about using output buffering as a "band aid" fix  8)

 

Just some other lame band aid.

Link to comment
Share on other sites


function redirect($pURL) {
if (strlen($pURL) > 0) {
if (headers_sent()) {
echo "document.location.href='".$pURL."';\n"; }
else {
header("Location: " . $pURL);
}
exit();
}
}

example.php


redirect("''".WEB_ROOT."'/index.php");

 

then you don't have to worry about using output buffering as a "band aid" fix  8)

 

We're no longer talking about Output Buffering...

 

 

Debbie

 

 

Link to comment
Share on other sites

The answer is....  *drum roll*

 

In order to populate the GET array, you have key-value pairs - preceded by a question mark - in the URL like this...

 

?title=postage-meters-can-save-you-money

 

 

My original Session variable was like this...

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

 

And so it created a return to URL like this

 

/articles/postage-meters-can-save-you-money

 

... which wouldn't create a GET value and thus my code didn't have somewhere to re-direct to and defaulted to "index.php"

 

 

If I change my Session variable to this...

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

 

...the problem appears to have been fixed?!

 

Come on experts...  ;)

 

 

Debbie

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.