Cookies and Sessions
Posted: Sun Jan 19, 2003 1:52 pm
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
Here is the top part of my code from line 1 to line 39Warning: 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
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
}
}
?>