Page 1 of 1

Cookies and Sessions

Posted: Sun Jan 19, 2003 1:52 pm
by oldtimer
I have got sessions down pat and work great. However now I am having to learn cookies. I can currently set a cookie and then get the information from it. But should I use sessions with the cookies or just make sure the cookie is there at each page? For example now I have session_start(); on each page and a check to see if they are logged in. Should I do away with that and just have the cookie check. I have tried using the 2 together and
Warning: Cannot add header information - headers already sent by (output started at /var/www/thefolder/new/index.php:11) in /var/www/thefolder/new/index.php on line 32

Warning: Cannot add header information - headers already sent by (output started at /var/www/thefolder/new/index.php:11) in /var/www/thefolder/new/index.php on line 33

Warning: Cannot add header information - headers already sent by (output started at /var/www/thefolder/new/index.php:11) in /var/www/thefolder/new/index.php on line 34
Here is the top part of my code from line 1 to line 39

Code: Select all

<?php
session_start();
include("myconfig.php");
if (empty($_SESSION['valid_user'])) 
{ 
	if (isset($_COOKIE["check"])) 
 {
// TestCookie exists
 // now we get the rest of their information
 $login=$_COOKIE["valid_user"];
 $email=$_COOKIE["email"];
 echo "login is $login and email is $email<BR>";
 mysql_connect($DBhost,$DBuser,$DBpass) or
 die("Unable to connect to database");
 @mysql_select_db("webthings") or die("Unable to select
 database");
  $sqlquery = "SELECT * From wt_users where name='$login' and email='$email'";
 $result=mysql_query($sqlquery);
 $num=mysql_num_rows($result);
 $i=0;
 while ($i < $num) { 
 $valid_user=mysql_result($result,$i,'name');
 $valid_user=htmlspecialchars($valid_user);
 $email=mysql_result($result,$i,'email'); 
  $fast=mysql_result($result,$i,'approved');
   $trusted=mysql_result($result,$i,'password');
 ++$i;} 
    session_register("valid_user", "email", "fast", "trusted");
	// update cookie for time expiration
setcookie("valid_user","$valid_user",604800,"/","www.coastgames.com");
setcookie("email","$email",604800,"/","www.coastgames.com");
setcookie("check","valid_user",604800,"/","www.coastgames.com");
 } 
else
 {
// TestCookie does NOT exist  do nothing
 }
 }


?>

Posted: Sun Jan 19, 2003 3:05 pm
by Kriek
Perhaps you could use output buffering with ob_start() and ob_end_flush()

Code: Select all

<?php ob_start(); ?>

// Your script here

<?php ob_end_flush(); ?>
As Elmseeker pointed out: Although ob_end_flush() isn't needed in MOST cases because it is called automatically at the end of script execution by PHP itself when output buffering is turned on either in the php.ini or by calling ob_start()

Posted: Mon Jan 20, 2003 2:46 am
by twigletmac
Also have a read of this:
viewtopic.php?t=1157

Mac