Cookies and Sessions

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
oldtimer
Forum Contributor
Posts: 204
Joined: Sun Nov 03, 2002 8:21 pm
Location: Washington State

Cookies and Sessions

Post 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
 }
 }


?>
User avatar
Kriek
Forum Contributor
Posts: 238
Joined: Wed May 29, 2002 3:46 am
Location: Florida
Contact:

Post 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()
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Also have a read of this:
viewtopic.php?t=1157

Mac
Post Reply