Valhalla Legends Forums Archive | Web Development | [PHP] setcookie() error

AuthorMessageTime
j0k3r
Ok I'm trying to set a cookie to remember the user has logged in, which will enable him to do things on the site, but I get an error that claims I have text being outputted before the cookie is set, when this is clearly not the case, anyways code+error.

[code]<?php
$name = $_POST['name'];
$password = $_POST['password'];

include 'somedatabasefile.php';

function verifylogin ($nametesting, $passwordtesting)
{
   global $verified;
   $verified = false;
   $result = mysql_query("SELECT * FROM users;");   
   while ($row = mysql_fetch_array($result))
   {
      if ($nametesting == $row[0] && $passwordtesting == $row[1])
      {
         $verified = true;
      }
   }
}

verifylogin($name, $password);

if (!is_null($name) && !is_null($password) && $verified)
{
   setcookie("userid", "$name");
}

mysql_close($dbh)
?>[/code]
[code]Warning: Cannot modify header information - headers already sent by (output started at /some/path/public_html/somedatabasefile.php:7) in /home/template/public_html/login.php on line 25[/code]

What's the problem?
February 25, 2004, 11:40 PM
Adron
Hmm, so what's in somedatabasefile.php? It doesn't start the output going?
February 25, 2004, 11:43 PM
j0k3r
It says it started in somedatabasefile's line 7, well here's the code for that too.
[code]<?php
$dbh=mysql_connect ("place", "username", "password") or die ('I cannot connect to the database because: ' . mysql_error());
mysql_select_db ("somedatabase");
?>[/code]

[edit]The reason I do it like this is because I do alot of this stuff at school, and I don't want people peeking at my username/password for the database.
[/edit]
[edit2]For those of you who are slightly interested, here's all you need to know about PHP and cookies. http://ca2.php.net/manual/en/function.setcookie.php[/edit2]
February 25, 2004, 11:45 PM
j0k3r
I came back an hour later, and it works, maybe I wrote some sort of self-writing code?
February 26, 2004, 3:53 AM
drivehappy
I prefer to use $_SESSION[], it is much easier.
February 29, 2004, 2:04 AM
j0k3r
[quote author=drivehappy link=board=22;threadid=5455;start=0#msg46613 date=1078020273]
I prefer to use $_SESSION[], it is much easier.
[/quote]
But I need this to work for next time they visit the site, which means they could close the browser/restart the computer/whatever and it needs to still work. Not very nice having to sign in every time or keep your browser open 24/7 is it?
February 29, 2004, 3:00 AM
St0rm.iD
[quote author=j0k3r link=board=22;threadid=5455;start=0#msg46091 date=1077767612]
I came back an hour later, and it works, maybe I wrote some sort of self-writing code?
[/quote]

That's BAD.
February 29, 2004, 3:10 AM
j0k3r
[quote author=St0rm.iD link=board=22;threadid=5455;start=0#msg46631 date=1078024200]
[quote author=j0k3r link=board=22;threadid=5455;start=0#msg46091 date=1077767612]
I came back an hour later, and it works, maybe I wrote some sort of self-writing code?
[/quote]

That's BAD.
[/quote]
Not for me, I'm it's creator, it won't turn on me.







Right?
February 29, 2004, 3:19 AM
St0rm.iD
PHP was invented by Europeans. Watch out.
March 1, 2004, 12:18 AM
j0k3r
So? I created it's intelligence.
March 1, 2004, 2:08 AM

Search