Jump to content

Modify Header Error - Caused By 'require?


oliverj777

Recommended Posts

Hello,

 

I have a simple login script that basically, when if you go to a page and you are NOT logged in (saved in cookie) then you get redirected to the login page, else the content is shown.

 

It works, but I currently have my database connection details on the same page, I like to have them on a separate PHP and use require 'connet.php'. So I've gone ahead and done that, but now I get an error saying "Warning: Cannot modify header information - headers already sent". This only happens if I'm NOT logged in.

 

It has something to do with my require "connect.php"; because if I comment that out, it works fine. Here is the script:

 

<?php 

//require "connect.php"; // < causes header error

// Connects to your Database 
mysql_connect("localhost", "user", "pass") or die(mysql_error()); 
mysql_select_db("db") or die(mysql_error()); 

//checks cookies to make sure they are logged in 
if(isset($_COOKIE['ID_my_site'])) { 
$username = $_COOKIE['ID_my_site']; 
$pass = $_COOKIE['Key_my_site']; 
$check = mysql_query("SELECT * FROM users WHERE username = '$username'")or die(mysql_error()); 
	while($info = mysql_fetch_array( $check )){ 
	//if the cookie has the wrong password, they are taken to the login page 
	if ($pass != $info['password']){ 			
		header("Location: index.php"); 
	} 
	//otherwise they are shown the admin area	 
	else{ 

?>
            
       - content -  
       <a href=logout.php>Logout</a>   
            
<?
		} 
	}
} 	
//if the cookie does not exist, they are taken to the login screen 
else{			 
header("Location: index.php"); 
} 
?> 

 

My connect.php page is just:

 

<?
// Connects to your Database 
mysql_connect("localhost", "user", "pass") or die(mysql_error()); 
mysql_select_db("db") or die(mysql_error()); 
?>

 

Why is the require causing this error, and how do I fix it?

 

Thanks

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.