-1

I have been struggling with this error for some time now.

I have created a profile site, If I run it localy there is no problem to login and get redirect to the profile page.

But at soon I upload it, nothing happends when i press the login . I just get an empty page ... But if i open the page source I can se that its reading my init.inc.php file. but nothing more than the comment code is readed.. And since i wont get any error message I really dont know whats the problem.

aaa hope you get what Im trying to say :)

This is my login page

<?php 
 include 'core/inc/init.inc.php';
 if ($_SERVER['HTTP_HOST'] != 'localhost') // running in remote server
    $server = 'pixeltouch2.mysql.domeneshop.no';  
    else // running locally
    $server = 'pixeltouch2.mysql.domeneshop.no';

mysql_connect($server, "LOGIN", "pass") or die(mysql_error());
  mysql_select_db("pixeltouch2") or die(mysql_error()) ;


$errors = array();

if (isset($_POST['username'], $_POST['password'])){
    if(empty($_POST['username'])){
        $errors[] = 'The username cannot be empty.';    
    }
        if(empty($_POST['password'])){
        $errors[] = 'The password cannot be empty.';
    }
//Log in
    if (valid_credentials($_POST['username'], $_POST['password']) == false ){
        $errors[] = 'Username / Password incorrect.';
    }
        if (empty($errors)){
            $_SESSION['username'] = htmlentities($_POST['username']);
            $_SESSION['uid'] = fetch_user_id($_SESSION['username']);

            header("Location: profile.php?uid=" . $_SESSION['uid']);
                die();
                echo $_SESSION['uid'];
        }
}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 
<title>Pixeltouch</title> 

<link rel="stylesheet" type="text/css" href="ext/style.css" />

</head> 
<body> 
<div id="page-wrap"> 
<div id="main-content"> 
<br/>
<?php include_once('template/head.inc.php');
?>
<div id="menu"> 

<?php include_once('template/nav.inc.php');
?> 

</div> 
<!-- SKRIVBOX-->

<div>
<?php 
if(empty($errors) == false){
?>
<ul>

<?php

foreach ($errors as $error) {
    echo "<li>$error</li>";
}

?>
</ul>

<?php

}else{
echo 'Need an account ? <a href="register.php">Register here</a>'; 
}

?>



</div>
<br/>
<form method="post" action="" >
<p>
<label for="username">Username:</label>
<input type="text" name="username" id="username" value="<?php if (isset($_POST['username'])) echo  htmlentities($_POST['username']); ?>" />
</p>
<p>
<label for="password">Password: </label>
<input type="password" name="password"  id="password" />
</p>
<p>
<input type="submit" value="Login" />
</p>

</form>


<!-- SKRIVBOX END-->
</div> 

<?php include_once('template/foot.inc.php');
?>
</div>
</body>
</html>

And my init.inc.php file

     <?php
     session_start();

/*
     mysql_connect("pixeltouch2.mysql.domeneshop.no", "LOGIN", "PASS") or die(mysql_error()) ; 
     mysql_select_db("pixeltouch2") or die(mysql_error()) ;
     */

     if($_SERVER['HTTP_HOST'] != 'pixeltouch2.mysql.domeneshop.no')// running in remote server
        $server = 'pixeltouch2.mysql.domeneshop.no';  
        else // running locally
        $server = 'pixeltouch2.mysql.domeneshop.no';

    mysql_connect($server, "login", "pass") or die(mysql_error());
      mysql_select_db("pixeltouch2") or die(mysql_error()) ;

     $path = dirname(__FILE__);
     include("{$path}/user.inc.php");

     ?>


                                        <!--Registration/Login (START)-->
     <?php 


     $exceptions = array('register', 'login', 'user_list', 'profile', 'edit_profile', 'upload');

     $page = substr(end(explode('/', $_SERVER['SCRIPT_NAME'])), 0, -4);

     if(in_array($page, $exceptions) == false){
        if (isset($_SESSION['username']) == false){
        header('Location: login.php');
        die();
        }
      }
     ?>

                                        <!--Registration/Login (END)--> 


                                            <!--User Profile (START)-->
     <?php
     $_SESSION['uid'] = 1;


     ?>

                                            <!--User Profile (END)-->

Error LOG

Warning: Cannot modify header information - headers already sent by (output started at /home/3/p/pixeltouch/www/book/core/inc/init.inc.php:2) in /home/3/p/pixeltouch/www/book/login.php on line 32

When I look @ line 32 I its my  header("Location: profile.php?uid=" . $_SESSION['uid']);
                die();
                echo $_SESSION['uid'];

So its something about the session, is there another way to write the code so it work ?

Dymond
  • 2,158
  • 7
  • 45
  • 80
  • 1
    Are you sure you uploaded it to a php server? :) – Hans Wassink May 25 '12 at 13:38
  • 3
    Please use at the beginning of all files `error_reporting(E_ALL);` and `ini_set("display_errors", 1);` to display the errors – David May 25 '12 at 13:42
  • 3
    Why are you repeting the mysql_connect in the two PHP files? And why are the login and password different??? Also: **don't put here real login/pass information**, please! – felixgaal May 25 '12 at 13:44
  • Both your server variables is declared as 'pixeltouch2.mysql.domeneshop.no' for localhost and public server. is connection details correct? I suggest you reupload to everything to ensure everything(updated) is uploaded. – Slay May 25 '12 at 13:46
  • Same comment as your recent [other question](http://stackoverflow.com/questions/10726151/another-500-internal-server-php) - What, do your, server logs, say??? If you're not reporting errors to the PHP page, of course you will 'see no errors on the PHP page'. Look at your server logs! – PenguinCoder May 25 '12 at 13:48
  • Oh. Sorry =( Im all stressed by this so I didnt read my post twice before i send. Thank you for the tips about error_reporting . I get a error message that says I updated the question with the error message. – Dymond May 25 '12 at 14:17
  • Problem solved : by removing the session_start(); from the init file, and then added ob_start(); ?> at the top of my site and ob_flush(); ?> at the bottom . Thanks alot for the help. specially @David that gave the error_reporting tips! – Dymond May 25 '12 at 14:52

1 Answers1

1

Well the error says it right there. You are already sending output to the browser, and by saying "output" it means anything sent to the browser that are not http headers.

So look in your file for:

  • echo/print statements before your header location statement.
  • whitespaces or newline charachters before your <?php tags (even in included files).
  • Using the byte-order mark (BOM) at the beginning of a page.

So.. looking at your code samples I see (The + sign indicates spaces)

++++<?php
      session_start();

You need to correct that use

<?php
  session_start();
// No spaces at the beginning

Or even in this part

 // You close the php tag
 ?>++++++++++
 ++++<!--Registration/Login (START)-->+++++++
 <?php 
 $exceptions = array('register', 'login', 'user_list', 'profile', 'edit_profile', 'upload');

You dont need to do that, and you are sending a html comment.

To avoid such problems you could always use output buffering to ensure no output gets sent before the headers.

mpratt
  • 1,598
  • 12
  • 21
  • Hello. Thanks. I actually solved the problem by removing the session_start(); from the init file, and then added ob_start(); ?> at the top of my site and flush at the bottom, I dont know if its right way to do it, but i works. Thanks – Dymond May 25 '12 at 14:51
  • 1
    By the code sample you are giving, if you remove the `session_start` in your init.inc.php the `isset($_SESSION['username'])` is always going to evaluate to false. If you are already using __output buffering__, then there is no need to remove the `session_start` code – mpratt May 25 '12 at 14:56
  • You are right, i put it back and now my sql that is connect to sertain user works :) – Dymond May 25 '12 at 15:00
  • Hey! Dont forget to mark the question as Solved if the answer was helpful. [You can see how to do it here](http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work). – mpratt May 25 '12 at 17:33