Cookie Problem
| From: | The Head Sage | Date: | Sat, 19 Apr 2003 00:16:31 +0000 |
| Subject: | Cookie Problem | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-144448@lists.php.net to get a copy of this message | ||
Hey,
I've got a problem with cookies.. Here's the code for my login page..
<?php
//Grab the files with all the configuation amd functions
require("corelayout/header.php");
require("corelayout/function.php");
//Check to see if there is a value in the get variable
if (!empty($_GET['mode']))
{
$st_login_mode = $_GET['mode'];
}
else
{
$st_login_mode = "NULL";
}
//Check to see if there is a value in the Post variable
if (!empty($_POST['username']))
{
$st_login_user = $_POST['username'];
}
//Check to see if there is a value in the Post variables
if (!empty($_POST['password']))
{
$st_login_pass = $_POST['password'];
}
//If the mode is set to login, then run the login scripts, otherwise logout
if($st_login_mode == "login")
{
// Run the Login function which is in the functions.php file.
login();
}
elseif($st_login_mode == "logout")
{
//Destroy the cookie.
setcookie( "userid", "0", time () - 36000, "/",
$_SERVER['SERVER_NAME'], 0 );
$st_login_suceed = "3";
}
else
{
$st_login_suceed = "0";
}
?>
And here's the login(); function, which sits in function.php
<?php
function login()
{
//Set the variables needed by the function
global $st_global_db, $st_login_user, $st_login_pass, $st_login_user_text,
$st_login_pass_text, $st_login_suceed, $st_login_info;
//Connect to the MySQL server
mysql_connect($st_global_db['location'], $st_global_db['username'],
$st_global_db['password'])
or die ("Function login couldn't connect to database");
mysql_select_db($st_global_db['database'])
or die ("Function login couldn't select database");
//Check to see if the Username Exists
$st_login_user_sql = "SELECT * FROM st_users WHERE Username = '$st_login_user'";
$st_login_user_result = mysql_query($st_login_user_sql) or die ("Function login SQL
Error: ".mysql_error());
$st_login_user_num = mysql_num_rows($st_login_user_result);
if ($st_login_user_num == "1")
{
//Username exists so inform the user.
$st_login_user_text = "Username Confirmed";
//Check to see if the password matches
$st_login_pass_sql = "SELECT * FROM st_users WHERE Username = '$st_login_user' AND
Password = '$st_login_pass'";
$st_login_pass_result = mysql_query($st_login_pass_sql) or die ("Function login SQL
Error: ".mysql_error());
$st_login_pass_num = mysql_num_rows($st_login_pass_result);
$st_login_info = mysql_fetch_array($st_login_pass_result);
if ($st_login_pass_num == "1")
{
//Password correct so inform the user.
$st_login_pass_text = "Password Confirmed";
$st_login_suceed = "1";
setcookie("userid", $st_login_info['UserID'], time () + 36000, "/",
$_SERVER['SERVER_NAME'], 0 );
}
else
{
//Password failed, so inform the user.
$st_login_pass_text = "<b>Error</b>: Password incorrect, please go back and try
again.";
$st_login_suceed = "2";
}
}
else
{
//Username not found.
$st_login_user_text = "<b>Error</b>: Username not found, or Username entered
incorrectly. Please go back and enter it correctly";
$st_login_suceed = "2";
}
//Close the connection
mysql_close();
}
?>
But when ever i attempt to either login or logout, the cookies wont send giving me the following
error
<b>Warning</b>: Cannot modify header information - headers already sent by (output
started at e:\htdocs\sagestower\login.php:1) in
<b>e:\htdocs\sagestower\corelayout\function.php</b> on line <b>44</b>
Any suggestions? Sorry for the length of this e-mail ^^;
- Daniel "TheHeadSage" Spain
theheadsage[at]manga-sketchbook[dot]org
Founder of Voidsoft
System Admin for The Void
Technical Admin
*/ Join #php on irc.mangaworld.org:7000 /*