Hello Guest, please login or register.
Did you miss your activation email?
Login with username, password and session length.

Pages: [1]   Go Down

Author Topic: Switching to Cookies  (Read 883 times)

0 Members and 1 Guest are viewing this topic.

PoeFacedKilla

Prussian Killer Bee
Switching to Cookies
« on: May 20, 2007, 06:07:42 pm »
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Gender: Male
  • Posts: 269
I am switching from Sessions to Cookies on my site, and I was wondering why i am getting this error:
Warning: Cannot modify header information - headers already sent by (output started at C:\Program Files\xampp\htdocs\black_site\layout\top.php:8) in C:\Program Files\xampp\htdocs\black_site\login.php on line 16

Warning: Cannot modify header information - headers already sent by (output started at C:\Program Files\xampp\htdocs\black_site\layout\top.php:8) in C:\Program Files\xampp\htdocs\black_site\login.php on line 17

Warning: Cannot modify header information - headers already sent by (output started at C:\Program Files\xampp\htdocs\black_site\layout\top.php:8) in C:\Program Files\xampp\htdocs\black_site\login.php on line 18

Warning: Cannot modify header information - headers already sent by (output started at C:\Program Files\xampp\htdocs\black_site\layout\top.php:8) in C:\Program Files\xampp\htdocs\black_site\login.php on line 19

Here is my new code:
Code: [Select]
<? include $_SERVER['DOCUMENT_ROOT']."/black_site/layout/top.php"; ?>
<?
include "layout/conb.php";
$username = $_POST['username'];
$password = $_POST['password'];
$query = sprintf("SELECT `username`, `password`, `email`, `class` FROM `users` WHERE `username`='$username' AND `password`='$password'");
$result = mysql_query($query);
while ($row = mysql_fetch_assoc($result)) {
if(mysql_num_rows($result)==0) {
print "Incorrect User/Pass";
} else if(mysql_num_rows($result)==1) {
$username = $row['username'];
$password = $row['password'];
$email = $row['email'];
$class = $row['class'];
setcookie("user", "$username", time()+3600);
setcookie("pass", "$password", time()+3600);
setcookie("email", "$email", time()+3600);
setcookie("class", "$class", time()+3600);
print "I hope this Works!";
}
 }
 ?>
 <? include $_SERVER['DOCUMENT_ROOT']."/black_site/layout/bottum.php"; ?>
Logged
the Indyboard - User Generated Social Forum | Now With Even More Discussion!
Poe, The Independent Programmer
  • Zelda Shrine
Re: Switching to Cookies
« Reply #1 on: May 20, 2007, 06:09:03 pm »
  • *
  • Reputation: +3/-0
  • Offline Offline
  • Gender: Male
  • Posts: 6629
Cookies can only ever be set / got at the start of a script before anything has been emitted, even a space.
Logged
Re: Switching to Cookies
« Reply #2 on: May 20, 2007, 06:16:33 pm »
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Gender: Male
  • Posts: 1645
Yeah what infinitus said. But it's hard to put your set cookies line into the top because of it's variables, what you can do is using a form that is like <form action="./login.php">. Just that when you login it will load the same page and activate the other part of the in the begin, in example using an if(isset($_POST['blabla'])){//setcookie()} or something like that.
Logged
  • Virtual Security

mit

Re: Switching to Cookies
« Reply #3 on: May 20, 2007, 06:22:11 pm »
  • QBASIC programmer since age 4. Take that, world.
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Posts: 1079
Or you can use the output buffer to hold stuff before it's sent to the browser, and then send it after you've sent the cookie headers. Although I'd say that's generally bad practice.
Logged
Programmer / Spriter / Level designer / Game Director / Web Designer / Music Sequencer for
Random Highscore table:

Play the Kousou Arcade today!
  • Kousou Games
Re: Switching to Cookies
« Reply #4 on: May 21, 2007, 01:22:19 am »
  • IBV
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Gender: Female
  • Posts: 1075
Or you can use the output buffer to hold stuff before it's sent to the browser, and then send it after you've sent the cookie headers. Although I'd say that's generally bad practice.

Well, it's not bad practice, and it's actually one of it's uses but output buffers can be used for more than that. It can be used to seperate different parts of the page, and still execute non-printing code between, if needed.

Generally you should build your application from the ground like this:
1. Work with the request.
2. Send headers, cookies and so on.
3. Collect all data to be printed from e.g. a database.
4. Process the data and put it in an array, ready to be printed.
5. Print the data in the array.

Just remember that the cookies do NOT have to be at the very very top. You just have to put it before any code which print/echo information. Printing or echoing any information will automatically force the sending of headers (where cookies are stored). Like mit said, if you really have to print or echo before sending cookies you can use so called output buffers (ob_* functions, I think), because calling echo or print with ob activated will not actually send the data immediately, but will only do it when you call ob_flush() (which you put after the cookies).
Logged
My signature is empty.
Pages: [1]   Go Up

 


Contact Us | Legal | Advertise Here
2013 © ZFGC, All Rights Reserved



Page created in 0.187 seconds with 48 queries.

anything