Log in

View Full Version : PHP DATEBASE CONNECTION HELP!



07joeh07
24-07-2010, 06:46 PM
Hi everybody,

Need help everytime i go to my index.php web page this appears?





Warning: Cannot modify header information - headers already sent by (output started at /home/changeme/public_html/Game/includes/db_connect.php:1) in home/changeme/public_html/Game/index.php on line 7




am pretty sure i have done everythink right can i anybody tell me how this could fixed ? or could anybody try and fix it ?

Thanks Joe

Apolva
24-07-2010, 06:54 PM
Make sure your opening <?php tag is the VERY FIRST THING on the page.

07joeh07
24-07-2010, 06:55 PM
yess they are, any other reasons why its not working ?

Apolva
24-07-2010, 06:58 PM
Move any session_start() or header() calls before ANYTHING is outputted in the file or echo'd.

And do the same for db_connect.php

07joeh07
24-07-2010, 07:00 PM
am confusedd i really dont know how to do that ?

Apolva
24-07-2010, 07:01 PM
Paste me the contents of index.php and db_connect.php, minus any sensitive info, I'll take a look.

07joeh07
24-07-2010, 07:05 PM
index.php




<?php
session_start();
include_once("includes/db_connect.php");
if (strip_tags($_GET['logout']) == "yes"){
session_destroy();
}elseif (isset($_SESSION['real_name'])){
header("Location: Updates.php");
exit();
}


if ($_POST['Submit'] && strip_tags($_POST['username']) && strip_tags($_POST['password'])){
$username = $_POST['username'];
$password = $_POST['password'];
$username = strip_tags($username);
$password = strip_tags($password);
$ip = $REMOTE_ADDR;


$date = gmdate('Y-m-d h:i:s');


///check INFO
$sql = mysql_query("SELECT * FROM users WHERE username='$username' AND password='$password' LIMIT 1");
$login_check = mysql_num_rows($sql);
$inf = mysql_fetch_object($sql);

if ($login_check > "0"){
if ($inf->status == "Dead"){
include_once"dead.php";
exit();

$timenow=time();
$online = time() - 300; //the current time minus 300 seconds
$select = mysql_query("SELECT * FROM users WHERE onlinetime2 >='$online' AND online='Online' ORDER by id ASC");
$num = mysql_num_rows($select);
$numfor=number_format($num);

}
session_register('username');
$_SESSION['real_name'] = $inf->username;
$_SESSION["userlevel"] = $inf->userlevel;
$_SESSION["crewlevel"] = $inf->crewlevel;


$realip = isset($_SERVER['HTTP_X_FORWARDED_FOR']) ?
$_SERVER['HTTP_X_FORWARDED_FOR'] : $_SERVER['REMOTE_ADDR'];

$time2 = time();

$timestamp = time()+60;

mysql_query("UPDATE users SET online='Online', onlinetime2='$time2' WHERE username='$username'");

mysql_query("INSERT INTO `logs` ( `id` , `who` , `action` , `date` , `ip` ) VALUES ('', '$username', 'Logged In!', '$date', '$realip')");

header("Location: News.php");

} else {
$message= "You could not be logged in.<br />";

}}
?>

<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1">
<link REL="SHORTCUT ICON" type="image/ico" HREF="icon here">
<meta name="author" content="Bigharry">
<title>Mafia-Assassins</title>
<link href="site_css.css" rel="stylesheet" type="text/css">
<script src="clienthint.js"></script>
</head>

<body bgcolor="black" OnLoad="document.login.mail.focus();">
<div align="center">

<strong><font color='red' face='verdana' size='1'> <br>
<br>
</font></strong>
<table width="965" border="0" cellspacing="0" cellpadding="0">
<tr>
<td valign="bottom">
<table width="965" border="0" cellspacing="0" cellpadding="0">
<tr>

<td width="349" valign="bottom">
<table width="349" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="23"><div align="center"><img src="banner.jpg" width="966" height="168"></div></td>

</tr>
</table>
</td>


</tr>
</table></td>
</tr>

<tr>
<td>
<table width="965" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="200" valign="top" class="home-side">
<br>
<br>
<div class="brown"><center>Major Updates</center></div>
<div align="left" class="mafia-game">
<li>Refferal System has been fixed and updated.<br><br>

<li>The game layout has been slightly updated.<br><br>
<li>A blackjack casino is being made.<br><br>
</div>




<div align="left" class="mafia-game">
</div></td>
<td valign="top">
<table border="0" cellspacing="0" cellpadding="0" width="565" height="302" class="home-pic1">
<tr>

<td height="270" valign="top">
<table width="100%" border="0" cellpadding="0" cellspacing="0">
<tr>
<td height="168" valign="top"> <br>
<p class="home-text"><br>
<br>
<a href="Register2.php"><strong></strong></a>
<font size="-2"></font></p>
<strong><font color='red' face='verdana' size='1'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font></strong></td>

</tr>
<tr>
<td valign="bottom">

<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td>

<form id="form1" name="form1" method="post" action="index.php">



<div style="padding: 10px 0 0 400px;">

<input name="username" type="text" class="input-home" id="username" size="15" maxlength="35">

<br>
<br>
<input name="password" type="password" id="password2" class="input-home" size="15" maxlength="35">
<br>
<input type="submit" name="Submit" value="Login">
</div>

</form> </td>

</tr>
</table></td>
</tr>

</table>
<br>
<br>
<br>
<br>
<br>
<center>
<body style="margin: 0px" bgcolor="black" vlink="black" alink="black">

</body></center>
</td></tr></table>


<td width="200" valign="top" class="home-side">


<div class="mafia-game">
<br>

<div class="brown">Need help playing?</div><br>
We have added a guide to help new users to the game or users that are new to the mafia life.<br>

<strong><br>
</strong><br>
</div></td>
</tr><div align="center" class="style"><a href="lost_pass.php">Lost Password </a><label> :: </label><a href="Register2.php"> Register </a><label> :: </label><a href="tos.php"> Terms of Service </a>

<br>
<br>
<br>
<font color="lime"><b>Mafia-Assassins is Open.</b></font></div></center>
</table>







</td>
</tr>
<tr>
<td>


</script></body>
</html>


Db Connect.php

<?php


$mysql_server = "....";
$mysql_user = "...";
$mysql_password = "...";
$mysql_database = "...";





$connection = mysql_connect("$mysql_server","$mysql_user","$mysql_password") or die ("Unable to connect to MySQL server.");
$db = mysql_select_db("$mysql_database") or die ("Unable to select requested database.");


?>

---------- Post added 24-07-2010 at 08:13 PM ----------

thanks this is much appriciated

Apolva
24-07-2010, 07:16 PM
I think you must have empty lines above the opening <?php tag on db_connect.php, remove those and try.

07joeh07
24-07-2010, 07:20 PM
nope still no luck :(

Apolva
24-07-2010, 07:41 PM
That error is caused when the headers have already been sent to the browser and you try to add more to the header. You must have flushed them by sending some of the page.

Are you sure there are no empty lines or spaces before or after the <?php and ?> tags?

Blob
25-07-2010, 02:28 PM
Could it be due to your database information being incorrect, and echoing out the error.

LMS16
25-07-2010, 08:27 PM
I think you must have empty lines above the opening <?php tag on db_connect.php, remove those and try.

PHP ignores empty lines as long as there is no code on there and that the line before it is ended with a ;

So doubt tht would help

Lew.

Apolva
26-07-2010, 12:43 PM
PHP ignores empty lines as long as there is no code on there and that the line before it is ended with a ;

So doubt tht would help

Lew.

This is incorrect.

LMS16
26-07-2010, 02:38 PM
As with HTML, whitespace is ignored between PHP statements. This means it is OK to have one line of PHP code, then 20 lines of blank space before the next line of PHP code. You can also press tab to indent your code and the PHP interpreter will ignore those spaces as well.

I rest my case.

Lew.

Apolva
26-07-2010, 02:45 PM
As with HTML, whitespace is ignored between PHP statements. This means it is OK to have one line of PHP code, then 20 lines of blank space before the next line of PHP code. You can also press tab to indent your code and the PHP interpreter will ignore those spaces as well.

I rest my case.

Lew.

Please re-read my first answer which you were keen to disprove. The problem is caused by whitespace OUTSIDE the PHP tags, which is not parsed by the PHP engine and is outputted directly to the browser, flushing the headers.

LMS16
27-07-2010, 07:21 AM
Please re-read my first answer which you were keen to disprove. The problem is caused by whitespace OUTSIDE the PHP tags, which is not parsed by the PHP engine and is outputted directly to the browser, flushing the headers.

My bad I mis-read your post :)

Anyway to expand on appy's explanation go here: http://kb2.adobe.com/community/publishing/505/cpsid_50572.html

Lew.

Want to hide these adverts? Register an account for free!