Page 1 of 1

Problem with my remote server

Posted: Tue Jul 22, 2008 12:50 am
by sonicintoxicants
Hi! It's my first time using a forum like this for help, but I figured I'd give it a shot!

I've just finished developing a content management system using PHP to interact with MySQL and everything works fine on my local testing server (I'm using a WAMP server). However, upon putting everything up on my remote server (on GoDaddy, MySQL version 5.0) I've been getting some errors. I managed to fix the error on the public side of the site by reordering the code, but on the staff side of my site, I'm pulling these errors:

Warning: session_start(): Cannot send session cookie - headers already sent by (output started at /home/content/t/h/e/thenexus/html/staff.php:2) in /home/content/t/h/e/thenexus/html/includes/session.php on line 2

Warning: session_start(): Cannot send session cache limiter - headers already sent (output started at /home/content/t/h/e/thenexus/html/staff.php:2) in /home/content/t/h/e/thenexus/html/includes/session.php on line 2

Warning: Cannot modify header information - headers already sent by (output started at /home/content/t/h/e/thenexus/html/staff.php:2) in /home/content/t/h/e/thenexus/html/includes/functions.php on line 21


The code below is from 'session.php':
<?php
session_start();
?>

<?php
function logged_in() {
return isset($_SESSION['user_id']);
}

function confirm_login() {
if(!logged_in()) {
redirect_to("login.php");
}
}
?>


And the following code is from 'functions.php':
function redirect_to($location = NULL) {
// Redirects browser to a specified location
if ($location != NULL) {
header("Location:{$location}");
exit;
}
}


I'm thinking it's a problem with the 'header' function in PHP, but I could sure use some help figuring out how to fix it!

Re: Problem with my remote server

Posted: Tue Jul 22, 2008 1:02 am
by Stryks
More likley than not, the problem will be coming from staff.php.

How are you including session.php ? Is there any HTML passed before it, even a blank line?

[edit] , actually, it's not what I had first suspected ... still ... might hep to see the start of staff.php

Re: Problem with my remote server

Posted: Tue Jul 22, 2008 1:04 am
by sonicintoxicants
Thanks for the quick response...

Here is 'staff.php' in its entirety:

<!-- Staff Menu -->
<?php require_once("includes/session.php"); ?>
<?php require_once("includes/functions.php"); ?>
<?php confirm_login(); ?>
<?php include("includes/staff_header.php"); ?>

<table id = "structure">
<tr>
<td id = "navigation">
&nbsp;
</td>
<td id = "page">
<h2>Staff Menu</h2>
<p>Welcome to the staff area, <?php echo $_SESSION['username']; ?>.</p>
<ul>
<!-- <li><a href = "search_content.php">Manage search content</a></li> -->
<li><a href = "corporate_content.php">Manage corporate content</a></li>
<li><a href = "new_user.php">Add staff user</a></li>
<li><a href = "logout.php">Logout</a></li>
</ul>
</td>
</tr>
</table>
<?php require("includes/staff_footer.php"); ?>

Re: Problem with my remote server

Posted: Tue Jul 22, 2008 1:20 am
by Stryks
Try

Code: Select all

 
<?php 
   require_once("includes/session.php");
   require_once("includes/functions.php");
   confirm_login();
   include("includes/staff_header.php");
?>
Note the removal of the HTML comment. Also, there's no need to have php tags surrounding each php command. Just on at the start and the close tag at the end.

Let me know how you go.

Re: Problem with my remote server

Posted: Tue Jul 22, 2008 1:26 am
by sonicintoxicants
Strange, but cutting out the HTML comment and consolidating the PHP into a block helped reduced the errors to just this:
Warning: Cannot modify header information - headers already sent by (output started at /home/content/t/h/e/thenexus/html/includes/session.php:5) in /home/content/t/h/e/thenexus/html/includes/functions.php on line 21

Still looks like I'm getting a problem with the 'header' function though... Any ideas as to what the exact problem is so I can forestall future complications?

Re: Problem with my remote server

Posted: Tue Jul 22, 2008 1:28 am
by Stryks
Can you post the session.php file in it's entirety please?

Re: Problem with my remote server

Posted: Tue Jul 22, 2008 1:30 am
by sonicintoxicants
'session.php' in its entirety:
<?php
session_start();
?>

<?php
function logged_in() {
return isset($_SESSION['user_id']);
}

function confirm_login() {
if(!logged_in()) {
redirect_to("login.php");
}
}
?>

Re: Problem with my remote server

Posted: Tue Jul 22, 2008 1:35 am
by Stryks
Ok ... first we'll try to strip out the spaces passed between those php tags ...

Code: Select all

<?php
session_start();
 
function logged_in() {
return isset($_SESSION['user_id']);
}
 
function confirm_login() {
if(!logged_in()) {
redirect_to("login.php");
}
}
?>
Now ... the code from functions.php please.

Re: Problem with my remote server

Posted: Tue Jul 22, 2008 1:37 am
by sonicintoxicants
'functions.php' in its entirety:
<?php
// Function library

// General functions
function mysql_prep($value) {
// 'Cleans' values for MySQL use
$magic_quotes_active = get_magic_quotes_gpc(); // Checks if magic quotes are active
$new_enough_php = function_exists("mysql_real_escape_string"); // Checks PHP version
if ($new_enough_php) {
if ($magic_quotes_active) { $value = stripslashes($value); }
$value = mysql_real_escape_string($value);
} else {
if(!$magic_quotes_active) { $value = addslashes($value); }
}
return $value;
}

function redirect_to($location = NULL) {
// Redirects browser to a specified location
if ($location != NULL) {
header("Location:{$location}");
exit;
}
}

function confirm_query($result_set) {
// Confirms that MySQL query was possible
if(!$result_set) {
die("Database query failed: " . mysql_error());
}
}

// Corporate site functions
function corporate_navigation($sel_subject, $sel_page, $public = false) {
// Creates dynamic navigation with subjects and their respective pages beneath
$output = "<ul class = \"subjects\">";
$subject_set = get_all_subjects($public);

while ($subject = mysql_fetch_array($subject_set)) {
$output .= "<li";
if ($subject["id"] == $sel_subject['id']) { $output .= " class=\"selected\""; }
$output .= "><a href = \"edit_subject.php?subj=" . urlencode($subject["id"]) . "\">{$subject["menu_name"]}</a></li>";
$page_set = get_pages_for_subject($subject["id"], false);
$output .= "<ul class =\"pages\">";
while ($page = mysql_fetch_array($page_set)) {
$output .= "<li";
if ($page["id"] == $sel_page['id']) { $output .= " class=\"selected\""; }
$output .= "><a href = \"edit_page.php?page=" . urlencode($page["id"]) . "\">{$page["menu_name"]}</a></li>";
}
$output .= "</ul>";
}
$output .= "</ul>";
return $output;
}

function public_navigation($sel_subject, $sel_page, $public = true) {

//$output = "<ul class=\"subjects\">";
$output = "";
$subject_set = get_all_subjects($public);

while ($subject = mysql_fetch_array($subject_set)) {
if ($subject["id"] == $sel_subject['id']) {
$page_set = get_pages_for_subject($subject["id"], true);
$output .= "<ul class =\"caption\">";
while ($page = mysql_fetch_array($page_set)) {
$output .= "<li";
if ($page["id"] == $sel_page['id']){
$output .= " class=\"selected\"";
}
$output .= "><a href=\"index.php?page=" . urlencode($page["id"]) . "\">{$page["menu_name"]}</a></li>";
}
$output .= "</ul>";
}
}
$output .= "</ul>";
return $output;
}

function get_all_subjects($public = true) {
// Retrieves all subjects from database in order by position
global $connection;
$query = "SELECT *
FROM subjects ";
if ($public){
$query .= "WHERE visible = 1 ";
}
$query .= "ORDER BY position ASC";
$subject_set = mysql_query($query, $connection);
confirm_query($subject_set);
return $subject_set;
}

function get_pages_for_subject($subject_id, $public = true) {
// Retrieves all pages under each subject in order by position
global $connection;
$query = "SELECT *
FROM pages
WHERE subject_id = {$subject_id} ";
if ($public) {
$query .= "AND visible = 1 ";
}
$query .= "ORDER BY position ASC";
$page_set = mysql_query($query, $connection);
confirm_query($page_set);
return $page_set;
}

function get_subject_by_id($subject_id) {
// Retrieves subject information as an array given subject id
global $connection;
$query = "SELECT * ";
$query .= "FROM subjects ";
$query .= "WHERE id=" . $subject_id . " ";
$query .= "LIMIT 1";
$result_set = mysql_query($query, $connection);
confirm_query($result_set);
if ($subject = mysql_fetch_array($result_set)) {
return $subject;
} else {
return NULL;
}
}

function get_page_by_id($page_id) {
// Retrieves page information as an array given page id
global $connection;
$query = "SELECT * ";
$query .= "FROM pages ";
$query .= "WHERE id=" . $page_id . " ";
$query .= "LIMIT 1";
$result_set = mysql_query($query, $connection);
confirm_query($result_set);
if ($page = mysql_fetch_array($result_set)) {
return $page;
} else {
return NULL;
}
}

function get_default_page($subject_id){
// Get all visible pages
$page_set = get_pages_for_subject($subject_id, true);
if ($first_page = mysql_fetch_array($page_set)){
// return $first_page;
return NULL;
} else{
return NULL;
}
}

function find_selected_page() {
// Determines which subject or page is currently selected
global $sel_subject;
global $sel_page;
if (isset($_GET['subj'])){
$sel_subject = get_subject_by_id($_GET['subj']);
$sel_page = get_default_page($sel_subject["id"]);
} elseif (isset($_GET['page'])) {
$sel_page = get_page_by_id($_GET['page']);
$sel_subject = get_page_by_id($sel_page["subject_id"]);
} else {
$sel_subject = NULL;
$sel_page = NULL;
}
}

?>

Re: Problem with my remote server

Posted: Tue Jul 22, 2008 1:43 am
by Stryks
Hmmm ... nothing too obvious there. Have you tried this after making the change to session.php? What error do you get now?

Re: Problem with my remote server

Posted: Tue Jul 22, 2008 1:45 am
by sonicintoxicants
I have... now the error's slightly different. I'm getting:
Warning: Cannot modify header information - headers already sent by (output started at /home/content/t/h/e/thenexus/html/includes/session.php:13) in /home/content/t/h/e/thenexus/html/includes/functions.php on line 21

Re: Problem with my remote server

Posted: Tue Jul 22, 2008 1:50 am
by Stryks
Ok, basically the server is saying that some output is being sent to the browser before the header command is sent in the function redirect_to().

Sometimes servers can be very sensitive about even the smallest output, even spaces.

I suspect that session.php still holds the problem. Make sure there are no spaces or lines before or after the <?php ?> tags in session.php and let me know what you get.

Re: Problem with my remote server

Posted: Tue Jul 22, 2008 1:59 am
by sonicintoxicants
Wow... you're right about that sensitivity. Turns out there were two blank spaces after the closing PHP tag in 'session.php'.

Thanks for all the help!