-1

Possible Duplicate:
Headers already sent by PHP

I am using Joomla 2.5 I have developed a small form outside joomla and would like to use Joomla Authentication to restrict access to it.

I have created three files:

auth.php (it is present in joomla root)

<?php
define( '_JEXEC',1);
define('JPATH_BASE', dirname(__FILE__));
define( 'DS', DIRECTORY_SEPARATOR );
require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' );
require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php' );
/* Create the Application */
$mainframe =& JFactory::getApplication('site');
/* Make sure we are logged in at all. */
if (JFactory::getUser()->id == 0)
die("Access denied: login required.");
?>

Second is connect-db.php

    <?php
    $server = 'xxxxxxxxx';
    $user = 'xxxxxxxx';
    $pass = 'xxxxxxxxxxxx';
    $db = 'xxxxxxxxxxx';

    // Connect to Database
    $connection = mysql_connect($server, $user, $pass) 
    or die ("Could not connect to server ... \n" . mysql_error ());
    mysql_select_db($db) 
    or die ("Could not connect to database ... \n" . mysql_error ());


    ?>

Third is list_names.php its in the folder "list"

    <?php
    // connect to the database
include('../auth.php');
    include('connect-db.php');
    // get results from database
    $result = mysql_query("SELECT * FROM names") 
            or die(mysql_error());             
    // display data in table
    echo "<table border='0' cellpadding='10' cellspacing='1' width='650'>";
    echo "<tr> <th>ID</th> <th>Names</th><th>Delete</th></tr>";
    // loop through results of database query, displaying them in the table
    while($row = mysql_fetch_array( $result )) {
            // echo out the contents of each row into a table
            echo "<tr>";
            echo '<td width="20">' . $row['id'] . '</td>';
            echo '<td width="330">' . $row['Name'] . '</td>';
            echo '<td width="150"><a class="btn-del" href="delete_name.php?id=' .             $row['id'] . '">Delete</a></td>';
            echo "</tr>"; 
    } 
    // close table>
    echo "</table>";
    ?>
    <p><a class="btn-add" href="new_name.php">Add New name</a></p>

Now the problem is when I access the file I get this error:

Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at /home/xxxxxxxx/public_html/name/list/list_names.php:2) in /home/xxxxxxx/public_html/name/libraries/joomla/session/session.php on line 532

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /home/xxxxxxxx/public_html/name/list/list_names.php:2) in /home/xxxxxxxx/public_html/name/libraries/joomla/session/session.php on line 532 Access denied: login required.

I am not a programmer! I am learning by doing please help. Regards,

Community
  • 1
  • 1

3 Answers3

1

The headers already sent warning is caused by output being sent to the browser before HTTP headers are sent (in your case a cookie).

It looks like you have whitespace before the opening <?php tag in list_names.php and db-connect.php. Whitespace before the opening PHP tag will cause this warning, so you need to remove it.

Also, you are connecting to the db twice, you don't need to manually connect a second time if you are including Joomla, just grab Joomla's database object:

$db =& JFactory::getDBO();
MrCode
  • 63,975
  • 10
  • 90
  • 112
1

If you are using the following code

define( '_JEXEC',1);
define('JPATH_BASE', dirname(__FILE__));
define( 'DS', DIRECTORY_SEPARATOR );
require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' );
require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php' );
/* Create the Application */
$mainframe =& JFactory::getApplication('site');
/* Make sure we are logged in at all. */

There is no need of additional DB connection. you can user joomla DB object.

$db     = JFactory::getDBO();
$query = "SELECT * FROM table_name";
$db->setQuery($query);
$db->query();
$num    =   $db->getNumRows();
$result = $db->loadObject();

Hope this will help you..

Jobin
  • 8,238
  • 1
  • 33
  • 52
-1

Put your code into an authentication plugin following the instructions in the example.

Elin
  • 6,507
  • 3
  • 25
  • 47