Welcome to Dream.In.Code
Getting PHP Help is Easy!

Join 132,694 PHP Programmers for FREE! Get instant access to thousands of PHP experts, tutorials, code snippets, and more! There are 1,313 people online right now. Registration is fast and FREE... Join Now!




PHP header issue

 
Reply to this topicStart new topic

PHP header issue

musya
post 7 Jun, 2008 - 09:45 AM
Post #1


D.I.C Regular

Group Icon
Joined: 25 Apr, 2007
Posts: 291



Thanked 1 times

Dream Kudos: 50
My Contributions


I am using header to redirect to a different page and i get this error when the header line is crossed.
CODE

Warning: Cannot modify header information - headers already sent by (output started at /location/of/file/login.php:17) in /location/of/file/login.php on line 19


and here is the code area where it runs
php

if($result['password'] == $_POST['entered_password'])
{
echo 'login successful';
//setcookie("user", $result['id'], time()+3600);
header( 'Location: client/index.php' );
}

User is offlineProfile CardPM

Go to the top of the page

Martyr2
post 7 Jun, 2008 - 09:50 AM
Post #2


Programming Theoretician

Group Icon
Joined: 18 Apr, 2007
Posts: 5,062



Thanked 177 times

Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions


You can't print or echo anything prior to using the header function. This is because any time you use print or echo you are causing the response headers of the page to be sent to the user. Print and echo are actually writing to a stream so when you start writing it sends the headers of the page, making it too late to modify the headers using the header() function.

To solve this make sure you do not print ANYTHING prior to making a header() call. This means no printing or echoing any spaces, tabs, lines or anything which would cause the headers of the page to be sent.

So to fix your code, comment out the echo line and if you have no other echos previous to this code, it should work fine.

You can read up on this more at php.net for the header() function.

Hope this helps you out!

"At DIC we be header sending code ninjas... no I said 'heading'! Get your mind out of the gutter" decap.gif
User is offlineProfile CardPM

Go to the top of the page

musya
post 7 Jun, 2008 - 09:58 AM
Post #3


D.I.C Regular

Group Icon
Joined: 25 Apr, 2007
Posts: 291



Thanked 1 times

Dream Kudos: 50
My Contributions


yup that makes sense, actually the echo there is just for my testing purposes I'm going to write a cookie their instead i don't think that would be a problem?
Thank you.
I appreciate it,
especially with the fast response.
User is offlineProfile CardPM

Go to the top of the page

Martyr2
post 7 Jun, 2008 - 10:13 AM
Post #4


Programming Theoretician

Group Icon
Joined: 18 Apr, 2007
Posts: 5,062



Thanked 177 times

Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions


I don't imagine writing to a cookie would cause a problem. smile.gif
User is offlineProfile CardPM

Go to the top of the page

musya
post 7 Jun, 2008 - 12:06 PM
Post #5


D.I.C Regular

Group Icon
Joined: 25 Apr, 2007
Posts: 291



Thanked 1 times

Dream Kudos: 50
My Contributions


Okay now I have an awkward problem, my mysql results returns only the first user, so only 1 user is able to login, and im not sure why, and also in my foreach loop i used $result appose to $value since when i used $value i got gibberish back, and the echo statment is for my checking purposes.
Any help would be greatly appreciated, I also check my sql statement with phpmyadmin and it returns all the users like it should.

php

$result = mysql_fetch_array($row);
$match = false;
foreach($result as $value)
{
echo $result['username'].'------'.$result['password'];
if($result['username'] == $_POST['entered_name'])
{
if($result['password'] == $_POST['entered_password'])
{
$match = true;
}
}
}
User is offlineProfile CardPM

Go to the top of the page

JBrace1990
post 7 Jun, 2008 - 01:12 PM
Post #6


D.I.C Regular

Group Icon
Joined: 9 Mar, 2008
Posts: 474



Thanked 21 times

Dream Kudos: 350
My Contributions


if you're creating a login system, you should set the SQL to select 1 account where the username and the password are those typed by the user...
User is offlineProfile CardPM

Go to the top of the page

akozlik
post 7 Jun, 2008 - 01:36 PM
Post #7


D.I.C Addict

Group Icon
Joined: 25 Feb, 2008
Posts: 596



Thanked 22 times

Dream Kudos: 750
My Contributions


Post all your code. Specifically where your sql statement is.

Also, check the PHP tutorials for how to do a basic authentication system. It's all in there.

This post has been edited by akozlik: 7 Jun, 2008 - 01:37 PM
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 11/23/08 07:47AM

Live PHP Help!

PHP Tutorials

Reference Sheets

PHP Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month