Page 1 of 1

In need of help

Posted: Thu Oct 20, 2005 5:29 am
by Ree
I am having a hard time trying to fix (actually, trying to find what to fix) my script to run on my client's host. If you could give me a few hints what could be causing the problem, that would be greatly appreciated. Basically it's related to a session var.

The script structure of the backend of the site is VERY simple. I will not write the code which does db stuff, only the code related to my prob. All request scripts are composed the following way: all the processing code is written there and after all the required data is collected, a template (PHP + HTML) is included which itself includes appropriate content (also HTML + PHP) file. The template and content files are stored in /pages dir. The variable I am having problems with is $_SESSION['msg'] (it is used to store any message I need to print for the user). Each content page has this snippet embedded to echo $_SESSION['msg'] and then 'erase it':

Code: Select all

echo $_SESSION['msg'];
$_SESSION['msg'] = '';
So, on the login page I have:

Code: Select all

//Not yet logged in
session_start();
$_SESSION['msg'] = '';
include('pages/index.php'); //display template to user, since not logged in, echo $_SESSION['msg'] prints no messages
//exit

/* ... */

//If login is submitted and correct
//set some other session vars
header('location: home.php'); //redirect to home page of the backend
So far, $_SESSION['msg'] is empty, right? Fine, the next is home.php. It's nothing special (actualy the content area is empty in it):

Code: Select all

session_start();
//Check if logged in
//Some language switch code
include('pages/index.php');
$_SESSION['msg'] is still empty here. Now say, I want to add a news item in the db. So on the main menu, I click add_news.php (so now the order of visited pages is this: index.php (login) -> redirect to home.php -> add_news.php (clicked in menu)). add_news.php:

Code: Select all

session_start();
//Check if logged in
include('pages/template.php'); //template.php has include('pages/add_news.php'), that is, it includes content page (form in this case).
pages/add_news.php looks like this:

Code: Select all

//HTML code
<?php

echo $_SESSION['msg'];
$_SESSION['msg'] = '';

?>
//HTML form
//HTML code
So after all this, $_SESSION['msg'] is still an empty string. So echo $_SESSION['msg']; on add_news.php should not print anything visible, but it does. It prints 'Array'. I just can't understand why.

When I need to print some messages via echo $_SESSION['msg']; the messages sometimes have 'U' in front of them (it replaces the first letter), although in my files they are written ok. This doesn't happen with ALL messages, but with some it certainly does. In one case I only have 'U' instead of the whole message. I have all messages stored in a single file:

Code: Select all

//class.Database.php
$msg['NoDB'] = 'Unable to connect to database.';

//class.Authorization.php
$msg['InvalidLogin'] = 'Invalid username or password.';

//class.Validator.php
$msg['EmptyField'] = '<b>{field}</b> field is empty.';
$msg['InvalidField'] = '<b>{field}</b> field is invalid.';

//General

$msg['NewsInserted'] = 'New item inserted successfully.';
$msg['NewsUpdated'] = 'Item updated successfully.';
$msg['NewsDeleted'] = 'Item deleted successfully.';
$msg['MassNewsDeleted'] = 'Selected items deleted successfully.';
$msg['NewsNotFound'] = 'Requested news item does not exist.';

/* ... */
Most of the errors come from objects, they only return code ID (such as 'EmptyField'). So often I use this to set message for the user:

Code: Select all

$_SESSION['msg'] = $msg[$object->getError()];
The host itself runs PHP 4.3.11 (windows host), my code was developed and tested successfully on PHP 4.4.0 with NO problems at all. The code runs on a subdomain, but I think this shouldn't be a problem.

I hope someone might spot what could be causing the problem.

Posted: Fri Oct 21, 2005 12:43 pm
by J_Iceman05
when you get the output of 'Array' replace the "echo $_SESSION['msg'];" with "print_r($_SESSION['msg']);"
that would print the array, what ever it is. by doing this, you may see exactly what is in the variable. and be sure to double check your pages to see that you aren't setting it when it shouldn't be. let me know what happens / the output. and let's see how fast we can fix your problem