0
votes

I just came across a problem which seems pretty unusual to me.

I want to "include" a particular PHP script (loggedStatus.php) in my various pages.

Its contents are -

<?php

@session_start();

//$username,$password,$databaseName declared

if(isSet($_COOKIE['myCookie']))
{
    $dbConnection = mysql_connect("localhost",$username,$password);
    mysql_select_db($databaseName);
    $userLoggedIn = mysql_fetch_array(mysql_query("SELECT * FROM usersList WHERE sessionVal='".$_COOKIE['myCookie']."'"));
    mysql_close($dbConnection);
    if(!$userLoggedIn['userID']) { echo header("location:../"); exit(); }
}
else { header("location:../"); exit(); }

$relLink = "../";

?>

My site's folder structure is -

mainFolder

- social

- - index.php
- - profile.php
- - messages.php
- - album.php
- - more files...

- include

- - loggedStatus.php
- - more files...

At the top of my every page (index.php, profile.php, etc), I am "including" the above using include('../include/loggedStatus.php');

But whenever I do that, I find a space at the top before my menu bar appears. This occurs only in the webkit browsers (Chrome, Safari, Opera), and not in Firefox.

Everything else appears to be fine.

But the space disappears when I replace the include('../include/loggedStatus.php'); with the full script!

Am I missing something, or is it a bug in the webkit engine?

1
warning your code is vulnerable to sql injection attacks! - Daniel A. White
Are you referring to the value of the $_COOKIE['myCookie'] variable? - Debojyoti Ghosh
correct. that reflects user input. - Daniel A. White
I hadn't thought of that. Thanks for pointing it out. :-) - Debojyoti Ghosh

1 Answers

0
votes

include statements are relative to the file's location on the server. I'd avoid using relative paths for include statements because if your code structure ever changes it will break. Set up a defined string which relates to you working directory and include files using that. You can quickly change that defined path if your code every changes.

define("BASE_DIR", "/var/www/");
include BASE_DIR . "includes/loggedStatus.php";

Or look into an autoloader.

The reverse is true for header redirects, make these absolute.

header("location: /"); //Redirect to root page
header("location: /profile.php"); //Redirect to profile page

As for the space at the start of the page check every file and make sure it starts with <?php and not a space or tab or any other form of white space.

Also use View Source in the browser to try and track down where the white space is within your page.