User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the PHP section within the Web Development category of DaniWeb, a massive community of 422,417 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 5,120 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our PHP advertiser: Lunarpages PHP Web Hosting
Views: 796 | Replies: 4 | Solved
Reply
Join Date: Apr 2007
Location: New Zealand
Posts: 7
Reputation: doel.jangkrik is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
doel.jangkrik doel.jangkrik is offline Offline
Newbie Poster

"Cannot modify header information - headers already sent by..." error

  #1  
Jun 15th, 2008
I am trying to make a login page using php.

index.php contain form for "login" and "password" which will be check by "process.php"

My intention is: if the login or password is incorrect, an alert box will pop up telling the user the case, and redirect back to index.php

Here is the code for process.php

<?php
session_start();
include('db.php');

if(isset($_POST['submit'])) :
	// Username and password sent from signup form
	// Remove all HTML-tags and PHP-tags
	$username = strip_tags($_POST['username']);
	$password = strip_tags($_POST['password']);

	// Make the query a wee-bit safer
	$query = sprintf("SELECT ID FROM users WHERE username = '%s' AND user_password = '%s' LIMIT 1;", mysql_real_escape_string($username), mysql_real_escape_string($password));
	
	$result = mysql_query($query);
	if(1 != mysql_num_rows($result)) :
	// MySQL returned zero rows (or there's something wrong with the query)

		echo "<script>alert('Wrong login or password')</script>"; 
		header('Location: index.php');
	else :
		// We found the row that we were looking for
		$row = mysql_fetch_assoc($result);
		
		// Register the user ID for further use
		$_SESSION['member_ID'] = $row['ID'];
		header('Location: members-only.php');
	endif;
endif;
?>

I got the error message in "cannot modify header information". From the php textbox that I got, I know that the error was caused because I put "echo" code before "header"

Is there anyway to achieve what I want to do without putting anything before the header?

Thanks!
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jun 2007
Location: Valley Center, Kansas
Posts: 554
Reputation: kkeith29 is on a distinguished road 
Rep Power: 3
Solved Threads: 57
kkeith29's Avatar
kkeith29 kkeith29 is offline Offline
Posting Pro

Re: "Cannot modify header information - headers already sent by..." error

  #2  
Jun 15th, 2008
you can't use header() like that. you would need to make the javascript redirect using:

window.location = 'index.php';
Reply With Quote  
Join Date: May 2008
Location: Kingdom of Saudi Arabia
Posts: 25
Reputation: yara1 is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 1
yara1 yara1 is offline Offline
Light Poster

Re: "Cannot modify header information - headers already sent by..." error

  #3  
Jun 16th, 2008
hi

mmmmm



i meet this error many time

only start ur code from first line

dont leave lines b4 <?php
Reply With Quote  
Join Date: May 2008
Posts: 13
Reputation: phpuser is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 1
phpuser phpuser is offline Offline
Newbie Poster

Re: "Cannot modify header information - headers already sent by..." error

  #4  
Jun 18th, 2008
hi,

As in above reply you cant have to left any line before code starting

and one thing is that , you include file like include('db.php');

so you have to check if there is another session_start(); in your db.php file then also you get this same error.

check all thing and run it again you sure get output
Reply With Quote  
Join Date: Apr 2007
Location: New Zealand
Posts: 7
Reputation: doel.jangkrik is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
doel.jangkrik doel.jangkrik is offline Offline
Newbie Poster

Re: "Cannot modify header information - headers already sent by..." error

  #5  
Jun 18th, 2008
thanks everyone for your help!

I just use kkeith29 suggestion to redirect using javascript.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb PHP Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the PHP Forum

All times are GMT -4. The time now is 9:35 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC