423,850 Members | 1,661 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 423,850 IT Pros & Developers. It's quick & easy.

Cannot modify header information - headers already sent by (output started at I:\xamp

P: 1
What is wrong with this code? This code has been given to me by one of my friends and told me to help him troubleshoot. I am using xampp 1.7.3 installed in the I: drive and my OS is win xp.
I created a .htaccess file and .htpass file in C drive under "C:\secret" folder.
.htaccess script -


Expand|Select|Wrap|Line Numbers
  1.   AuthUserFile "C:\secret\.htpass
  2.     AuthGroupFile /dev/null
  3.     Authame "Accounting Department"
  4.     AuthType Basic
  5.     Require valid-user

.htpass file contains the name and password.
Vars.inc contains this code-


Expand|Select|Wrap|Line Numbers
  1.   <?php
  2.             $host = "localhost";
  3.             $user = "root";
  4.             $passwd = "ABCD";
  5.             $database = "useraccount";
  6.     ?> 
  7.  
And there is "Welcome.inc" file with this code-


Expand|Select|Wrap|Line Numbers
  1.  <?php
  2.     /* File: Welcome.inc
  3.       * Desc: HTML code that displays the Welcome Web page.
  4.       *       Uses one PHP variable, $user_name.
  5.       */
  6.        echo "<html><head><title>Welcome</title></head>\n
  7.              <body>
  8.              <p align='center'>Hello, $user_name</p>\n
  9.              <p align='center'>Welcome to my secret page</p>\n
  10.              </body></html>";
  11.     ?>

I have created a database "useraccount" and a table "valid_user" in it with user_name,password ,create_date fields and then filled the "valid_user" table with data.
Now when I run the below code ,instead of a pop-window asking for username and password ,it shows directly "The User Name or password you entered is not valid." in the browser.


Expand|Select|Wrap|Line Numbers
  1. <?php
  2.     /* Program: Auth.php
  3.     * Desc:    Program that prompts for a user name and
  4.     *          password from the user using HTTP authentication.
  5.     *          The program then tests whether the user
  6.     *          name and password match a user name and password
  7.     *          pair stored in a MySQL database.
  8.     */
  9.  
  10.     if(!isset($_SERVER['PHP_AUTH_USER']))                   
  11.     {
  12.         header('WWW-Authenticate: Basic realm="secret section"');
  13.         header('HTTP/1.0 401 Unauthorized');                 
  14.         exit("This page requires authentication!");           
  15.     }                                                     
  16.  
  17.     else                                                   
  18.     {
  19.         include("Vars.inc");                                 
  20.         $user_name = trim($_SERVER['PHP_AUTH_USER']);       
  21.         $user_password = trim($_SERVER['PHP_AUTH_PW']);
  22.     $connection = mysql_connect($host,$user,$passwd)   #23
  23.                    or die ("Couldn't connect to server.");   
  24.         $db = mysql_select_db($connection,$database)
  25.                    or die ("Couldn't select database.");
  26.         $sql = "SELECT user_name FROM Valid_User
  27.                     WHERE user_name = '$user_name'
  28.                     AND password = md5('$user_password')";
  29.         $result = mysql_query($connection,$sql)
  30.                       or die("Couldn't execute query.");     
  31.         $num = mysql_num_rows($result);                     
  32.         if ($num < 1)         
  33.         {
  34.            exit("The User Name or password you entered
  35.                         is not valid.<br>");
  36.         }                                                     
  37.     }                                                       
  38. include("Welcome.inc");                                 
  39.     ?>
  40.  
Is the above code OK?
1)I have doubt as to whether the "passwd" will be "password" in line #23 above as the Vars.inc file contains "passwd" ?
2)Also will I have to put real values for $user,$host ,$password ,$database ?
3)should the $user be root which is my mysql username and the $password my mysql password?
4)The error is sometimes "The User Name or password you entered is not valid."
I referred to the articles in this same forum -
A)http://bytes.com/topic/php/answers/7...-php-line-10-a
B)http://bytes.com/topic/php/answers/7...-php-line-11-a
but still my problem remains.
Now the error is in line #23,the browser gives the following error -
Warning: mysql_connect() [function.mysql-connect]: Access denied for user 'user'@'localhost' (using password: YES) in I:\xampp\htdocs\test\test1\Auth.php on
line 23 Couldn't connect to server.
I am totally confused .
Please help.
Aug 22 '11 #1
Share this Question
Share on Google+
1 Reply


P: 93
use this function at the top of the file :

ob_start();
May 17 '12 #2

Post your reply

Sign in to post your reply or Sign up for a free account.