/    Sign up×
Community /Pin to ProfileBookmark

Same session code working differently in two folders

Hi, guys.

Sorry this got so long – please bear with me…

I have a session/login scripting setup that has been working fine for two clients’ folders, but I added the script to a new client and it’s not working for them.

Each of the clients sites have a header.php page, which has at the very top this code:

[code=php]<?
session_start();
// The included script checks the client IP address and if it is an allowed list the session variable will be set to the directory
// that the user tried to access
include(‘../scripts/checkIP.php’);
?>[/code]

The rest of the header.php file has the typical web page’s doctype declaration, the entire HEAD tag, and part of the body tag down to the menu.

This header.php file is itself included in each page on the clients Web site:

[code=php]<?php
$page_title = ‘Student Feedback’;
include(‘includes/header.php’);
?>

<h1>Home Page</h1>
<!– the rest of the page content below… –>[/code]

The checkIP script checks the client’s IP address and if it matches certain addresses, then the script sets a session variable and the client is allowed access to the folder without logging in. If the IP address doesn’t match then the client is redirected to login page. This allows me to access the folders from my computers without having to login. Pretty slick, if I may say so myself! :p

Here’s the checkIP script:

[code=php] // Get the client IP address
$ip = $_SERVER[‘REMOTE_ADDR’];

// Set this for testing logins, etc.
//$ip = ‘111.111.111.111’;

// echo ‘Client IP Address: <b>’ . $ip . ‘</b><br />’;

// Get the document’s folder name, which is the project folder for the user’s login
// $currentFolder = substr(dirname($_SERVER[‘PHP_SELF’]),1);
// echo ‘<br />$currentFolder variable is the folder this script is running from.<br />
// It was called by the user and is set to : <b>’ . $currentFolder . ‘</b><br />’;

// For testing/troubleshooting get the session variable being passed to the page
// $originalSessionValue = $_SESSION[‘folder’];
// echo ‘<br />Original session value: <b>’ . $originalSessionValue . ‘</b><br />’;

function CheckIP ($ip) {
if (($ip == ‘209.181.196.25’) || ($ip == ‘71.55.157.61’)) {
// The client IP address is in the list of allowed IPs
return true;
}
else {
return false;
}
}

if (CheckIP($ip)) {
// The function determined that the client IP address is allowed, so we’ll set the session variable to the document’s folder
// Which effectively bypasses having to log in
// echo ‘<br />This is echoed from the “if CheckIP is True” check,<br />
// The IP is allowed, so this block is setting SESSION folder to current folder: <b>’ . $currentFolder . ‘</b><br />’;

$_SESSION[‘folder’] = $currentFolder;

// $sessionFolderAfterIpCheck = $_SESSION[‘folder’];
// echo ‘$sessionFolderAfterIpCheck variable is set to: <b>’ . $sessionFolderAfterIpCheck . ‘</b> after being set to the modified SESSION folder value.<br />’;
}

// Find out if the session variable was changed in the function call from what it was originally
// $sessionFolder = $_SESSION[‘folder’];
// echo ‘<br />SESSION folder value after the “if CheckIP is True” check: <b>’ . $sessionFolder . ‘</b><br />’;

// If the IP address is not allowed, then either the session variable has already been set,
// or the user will be redirected to the login
if (($_SESSION[‘folder’] == ”) || ($_SESSION[‘folder’] != $currentFolder)) {
header(“Location: ../login.php”);

// $sessionFolderInRedirectCheck = $_SESSION[‘folder’];
// echo ‘<br />$sessionFolderInRedirectCheck: <b>’ . $sessionFolderInRedirectCheck . ‘</b><br />’;
// echo ‘<br />SESSION “folder” variable is empty or is not equal to $currentFolder (which would would be populated if
// IP address passed), so redirect to login page.<br />’;
}[/code]

It looks like a lot at first glance, but the majority of it is commented out. I’m using the commented code for troubleshooting. The amount of functional code is quite small in the script if you look closely.

As I said before, this checkIP/login setup works great for two existing client folders, but I just added a new client and it’s not working for the new folder. I’m getting these session errors:

[QUOTE]

Warning: session_start() [function.session-start]: Cannot send session cookie – headers already sent by (output started at […]/index.php:1) in […]/includes/header.php on line 2

Warning: session_start() [function.session-start]: Cannot send session cache limiter – headers already sent (output started at […]/index.php:1) in […]/includes/header.php on line 2

Warning: Cannot modify header information – headers already sent by (output started at […]/index.php:1) in […]/scripts/checkIP.php on line 48

[/QUOTE]

I know these messages happen when content is being output to the browser before setting a session (or something like that). I’ve gone over my code a bunch of time trying to find if I’m writing to the browser before the session, but I’m almost positive I’m not (but if not, then why would I be getting those errors?!).

The only difference between the new client and the previous, working client’s setup is the new client just has one web page, so I’m not using any includes except for the session start & checkIP.php include. I’ve event set up the new client’s page so that it was including a separate header page – to mimic the working clients’ setup, but I still got the error messages.

I don’t understand how I can get the errors from the one folder/page, but not from other folder/page paths even though they all call the same script file!

I’d really appreciate it if someone could offer some suggestions on how I can solve this.

to post a comment
PHP

11 Comments(s)

Copy linkTweet thisAlerts:
@criterion9May 20.2010 — It is possible that "<?" should be "<?php" depending on the configurations....also try checking for any white space between the opening php tag and the session_start call.
Copy linkTweet thisAlerts:
@eventideauthorMay 20.2010 — My heart skipped a beat when I saw that I was, indeed, missing the PHP after the <?.

But, alas, that didn't fix it.
Copy linkTweet thisAlerts:
@aj_nscMay 20.2010 — I can't see it being a parsing issue with the <? vs <?php as the parser is obviously parsing the session_start line.

I would say it is most likely whitespace in index.php (the file which includes header.php).

Anyway, the error is simple, something is being sent to the browser before session_start is called. It is most likely, as criterion pointed out, white space, but the point is, that it's something.
Copy linkTweet thisAlerts:
@eventideauthorMay 20.2010 — I totally agree. The errors indicate there's some sort of content before session_start is called.

Here's what I did: I copied the CheckIP code into the php script at the top of the page, replacing the include line, so I now have this at the beginning of the page:
[code=php]<?php session_start();

$ip = $_SERVER['REMOTE_ADDR'];
$currentFolder = substr(dirname($_SERVER['PHP_SELF']),1);

function CheckIP ($ip) {
if (($ip == '111.111.111.111') || ($ip == '222.222.222.222')) {
return true;
}
else {
return false;
}
}

if (CheckIP($ip)) {
$_SESSION['folder'] = $currentFolder;
}

if (($_SESSION['folder'] == '') || ($_SESSION['folder'] != $currentFolder)) {
header("Location: ../login.php");
}
?>[/code]


The rest of the page is just HTML.

Guess what - it [B]didn't[/B] work! Same errors. What the heck, man?!?! This is really weird. Especially since other files are using the same script...
Copy linkTweet thisAlerts:
@criterion9May 20.2010 — Are you using an IDE? Perhaps the BOM is being embedded?
Copy linkTweet thisAlerts:
@aj_nscMay 20.2010 — There's no problem with your header.php file, the error tells you there's a problem because there's content in your index.php file before you open your php tag.

For the record, the last warning about line 48 in checkIP.php is just saying the same thing as the first two - you can't redirect using header() because headers have already been sent. It's not another error, it's just the same problem.
Copy linkTweet thisAlerts:
@eventideauthorMay 20.2010 — the error tells you there's a problem because there's content in your index.php file before you open your php tag.[/QUOTE]

But there ain't I tell ya'!!! :p

I've attached the file to this post - see for yourself...

It's attached as a txt file, as the uploader won't allow php extensions.

[upl-file uuid=c6cb4951-4d4a-40fb-8936-bac870f18a90 size=4kB]index.txt[/upl-file]
Copy linkTweet thisAlerts:
@aj_nscMay 20.2010 — Then it's as criterion suggested, it's an invisible BOM.

Copy and paste your index.php file (the original one) into notepad++ and save it again. It should remove the BOM.

You could also copy and paste into notepad and save as with the encoding set as ANSI (default) instead of UTF-8.

No chance you're FTP-ing it as a binary file is it? Strange that it would work on other servers and not this one.
Copy linkTweet thisAlerts:
@eventideauthorMay 20.2010 — I'm using Dreamweaver MX2004 temporarily (I usually use DW CS3).

Don't forget that this same script works for two other folders on my server - the only (discernible) difference being those use includes.

I'm going to try the notepad thing and then ftp it using FileZilla.
Copy linkTweet thisAlerts:
@aj_nscMay 20.2010 — Taking a look at DWCS4 (I never actually used it myself) there's a checkbox under Unicode Normalization Form when you go to file and save as that says Include Unicode Signature (BOM).

You want to make sure that's unchecked for index.php. Better yet, always leave it unchecked, although it is by default.

Hopefully one of these things will help.
Copy linkTweet thisAlerts:
@eventideauthorMay 20.2010 — I must've been a BOM.

I opened the file in Notepad and saved it, then uploaded in FileZilla using the ASCII transfer setting and sure enough - no errors.

I'm thinking the reason this file caused problems and not the other is because I'm pretty sure I created those in DW CS3. At least, that's the only explanation I can think of...

Ah well - I hate the errors that are fixed and you never really know what caused the error in the first place! :rolleyes:

Thanks for all the help, guys.
×

Success!

Help @eventide spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.2,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,
)...