PHP Programming > Beginners - Learning PHP

Loop problems...

(1/2) > >>

dementedgimp:
hello,
I am having a problem looping with the do...while loop. Here is the code for cms_cookie1.php:

--- PHP Code: ---<?php
ini_set('display_errors', 1);
ini_set('log_errors', 1);
ini_set('error_log', dirname(__FILE__) . '/error_log.txt');
error_reporting(E_ALL);
function show_cookie($first_name,$middle_name,$last_name,$address,$city,$state,$zip_code,$phone,$user_name,$password,$email,$last_visit) 
{
echo $first_name."<br />";
echo $middle_name."<br />";
echo $last_name."<br />";
echo $address."<br />";
echo $city."<br />";
echo $state."<br />";
echo $zip_code."<br />";
echo $phone."<br />";
echo $user_name."<br />";
echo $password."<br />";
echo $email."<br />";
echo $last_visit."<br />";
}
function write_cookie() 
{
?>
<form action="<?php echo "fill_cookie.php"; ?>" method="POST">
First Name:<input type="text" name="first_name" value="<?php echo $_COOKIE['first_name']; ?>" size="15" maxlength="25"><br>
Middle Name:<input type="text" name="middle_name" value="<?php echo $_COOKIE['middle_name']; ?>" size="15" maxlength="25"><br>
Last Name:<input type="text" name="last_name" value="<?php echo $_COOKIE['last_name']; ?>" size="15" maxlength="25"><br>
Address:<input type="text" name="address" value="<?php echo $_COOKIE['address']; ?>" size="15" maxlength="25"><br>
City:<input type="text" name="city" value="<?php echo $_COOKIE['city']; ?>" size="15" maxlength="25"><br>
State:<input type="text" name="state" value="<?php echo $_COOKIE['state']; ?>" size="15" maxlength="25"><br>
Zipcode:<input type="text" name="zip_code" value="<?php echo $_COOKIE['zip_code']; ?>" size="15" maxlength="25"><br>
User Name:<input type="text" name="user_name" value="<?php echo $_COOKIE['user_name']; ?>" size="15" maxlength="25"><br>
Password:<input type="text" name="password" value="<?php echo $_COOKIE['password']; ?>" size="15" maxlength="25"><br>
Email:<input type="text" name="email" value="<?php echo $_COOKIE['email']; ?>" size="15" maxlength="25"><br>
Phone:<input type="text" name="phone" value="<?php echo $_COOKIE['phone']; ?>" size="15" maxlength="25"><br>
<input type="hidden" name="last_visit" value="<?php echo date('m/d/y'); ?>">
<input type="submit" name="enter" value="Enter">
</form>
<?php
}
function cms_delete_cookie() 
{
setcookie("first_name","",time()-1,"","192.168.1.2");
setcookie("middle_name","",time()-1,"","192.168.1.2");
setcookie("last_name","",time()-1,"","192.168.1.2");
setcookie("phone","",time()-1,"","192.168.1.2");
setcookie("address","",time()-1,"","192.168.1.2");
setcookie("city","",time()-1,"","192.168.1.2");
setcookie("state","",time()-1,"","192.168.1.2");
setcookie("zip_code","",time()-1,"","192.168.1.2");
setcookie("user_name","",time()-1,"","192.168.1.2");
setcookie("password","",time()-1,"","192.168.1.2");
setcookie("email","",time()-1,"","192.168.1.2");
setcookie("last_visit","",time()-1,"","192.168.1.2");
}
//Main
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/xhtml1-strict.dtd">
<head></head>
<body>
<?php
{
if(isset($_POST['submit'])) {
switch(strtoupper($_POST['choice'])) {
case "D" :
show_cookie($_COOKIE['first_name'],$_COOKIE['middle_name'],$_COOKIE['last_name'],$_COOKIE['address'],$_COOKIE['city'],$_COOKIE['state'],$_COOKIE['zip_code'],$_COOKIE['phone'],$_COOKIE['user_name'],$_COOKIE['password'],$_COOKIE['email'],$_COOKIE['last_visit']);
break;
case "C" :
cms_delete_cookie();
write_cookie();
break;
case "W" :
write_cookie();
break;
case "E" :
cms_delete_cookie();
break;
case "Q" :
break;
}
} else {
?>
<center>Cookie Manipulator</center>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<?php
if(isset($_COOKIE['last_visit'])) {
?>
<b>D</b>isplay Cookie<br>
<b>C</b>hange Cookie<br>
<b>E</b>rase Cookie<br>
<?php } ?>
<b>W</b>rite Cookie<br>
<b>Q</b>uit<br>
<input type="text" name="choice" size="1" maxlength="1">
<input type="submit" name="submit" value="submit">
<?php
}
 
?>
</form>
<?php
} while($_POST['submit'] != 'Q');
?>
</body>
</html>

--- End code ---

and here is the code for fill_cookie.php:

--- PHP Code: ---<?php
setcookie("first_name",$_POST['first_name'],time()+60*60*24*30,"","192.168.1.2");
setcookie("middle_name",$_POST['middle_name'],time()+60*60*24*30,"","192.168.1.2");
setcookie("last_name",$_POST['last_name'],time()+60*60*24*30,"","192.168.1.2");
setcookie("address",$_POST['address'],time()+60*60*24*30,"","192.168.1.2");
setcookie("city",$_POST['city'],time()+60*60*24*30,"","192.168.1.2");
setcookie("state",$_POST['state'],time()+60*60*24*30,"","192.168.1.2");
setcookie("zip_code",$_POST['zip_code'],time()+60*60*24*30,"","192.168.1.2");
setcookie("phone",$_POST['phone'],time()+60*60*24*30,"","192.168.1.2");
setcookie("user_name",$_POST['user_name'],time()+60*60*24*30,"","192.168.1.2");
setcookie("password",$_POST['password'],time()+60*60*24*30,"","192.168.1.2");
setcookie("email",$_POST['email'],time()+60*60*24*30,"","192.168.1.2");
setcookie("last_visit",$_POST['last_visit'],time()+60*60*24*30,"","192.168.1.2");
?>

--- End code ---

the errror when I run cms_cookie1.php is

--- Code: ---Notice: Undefined index: submit in /var/www/cms_cookie1.php on line 102 Notice: Undefined index: submit in /var/www/cms_cookie1.php on line 102 Notice: Undefined index: submit in /var/www/cms_cookie1.php on line 102 Notice: Undefined index: submit in /var/www/cms_cookie1.php on line 102 Notice: Undefined index: submit in /var/www/cms_cookie1.php on line 102 Notice: Undefined index: submit in /var/www/cms_cookie1.php on line 102 Notice: Undefined index: submit in /var/www/cms_cookie1.php on line 102 Notice: Undefined index: submit in /var/www/cms_cookie1.php on line 102 Notice: Undefined index: submit in /var/www/cms_cookie1.php on line 102 Notice: Undefined index: submit in /var/www/cms_cookie1.php on line 102 Notice: Undefined index: submit in /var/www/cms_cookie1.php on line 102 Notice: Undefined index: submit in /var/www/cms_cookie1.php on line 102 Notice:
--- End code ---
, until I halt it with ctrl-c. I am baffled!!!
Thanx ahead,
Jacques P.

richei:
Its nothing to worry about, just added if(isset()) to the variable and that message will go away.

ErnieAlex:
I think what Richei meant, was you can't handle a cookie if it doesn't exist.  Must test for that!

Also, why would you want to put that much info into a cookie?  It is not appropriate to put personal info into a cookie.  Tsk! Tsk! Tsk!  Someone on your site would be leaving their personal data on any computer that they were using..  Not the correct thing to do.  Use some other way to track your data, such as a database or SESSION variables...

dementedgimp:
OK dudes,
I put 2 if(isset...) statements in two of the functions, as follows:

--- PHP Code: ---<?php
ini_set('display_errors', 1);
ini_set('log_errors', 1);
ini_set('error_log', dirname(__FILE__) . '/error_log.txt');
error_reporting(E_ALL);
function show_cookie() 
{
if(isset($_COOKIE['last_visit'])) {
echo $_COOKIE['first_name']."<br />";
echo $_COOKIE['middle_name']."<br />";
echo $_COOKIE['last_name']."<br />";
echo $_COOKIE['address']."<br />";
echo $_COOKIE['city']."<br />";
echo $_COOKIE['state']."<br />";
echo $_COOKIE['zip_code']."<br />";
echo $_COOKIE['phone']."<br />";
echo $_COOKIE['user_name']."<br />";
echo $_COOKIE['password']."<br />";
echo $_COOKIE['email']."<br />";
echo $_COOKIE['last_visit']."<br />";
}
}
function write_cookie() 
{
?>
<form action="<?php echo "fill_cookie.php"; ?>" method="POST">
First Name:<input type="text" name="first_name" value="<?php echo $_COOKIE['first_name']; ?>" size="15" maxlength="25"><br>
Middle Name:<input type="text" name="middle_name" value="<?php echo $_COOKIE['middle_name']; ?>" size="15" maxlength="25"><br>
Last Name:<input type="text" name="last_name" value="<?php echo $_COOKIE['last_name']; ?>" size="15" maxlength="25"><br>
Address:<input type="text" name="address" value="<?php echo $_COOKIE['address']; ?>" size="15" maxlength="25"><br>
City:<input type="text" name="city" value="<?php echo $_COOKIE['city']; ?>" size="15" maxlength="25"><br>
State:<input type="text" name="state" value="<?php echo $_COOKIE['state']; ?>" size="15" maxlength="25"><br>
Zipcode:<input type="text" name="zip_code" value="<?php echo $_COOKIE['zip_code']; ?>" size="15" maxlength="25"><br>
User Name:<input type="text" name="user_name" value="<?php echo $_COOKIE['user_name']; ?>" size="15" maxlength="25"><br>
Password:<input type="text" name="password" value="<?php echo $_COOKIE['password']; ?>" size="15" maxlength="25"><br>
Email:<input type="text" name="email" value="<?php echo $_COOKIE['email']; ?>" size="15" maxlength="25"><br>
Phone:<input type="text" name="phone" value="<?php echo $_COOKIE['phone']; ?>" size="15" maxlength="25"><br>
<input type="hidden" name="last_visit" value="<?php echo date('m/d/y'); ?>">
<input type="submit" name="enter" value="Enter">
</form>
<?php
}
function cms_delete_cookie() 
{
if(isset($_COOKIE['last_visit'])) {
setcookie("first_name","",time()-1,"","192.168.1.2");
setcookie("middle_name","",time()-1,"","192.168.1.2");
setcookie("last_name","",time()-1,"","192.168.1.2");
setcookie("phone","",time()-1,"","192.168.1.2");
setcookie("address","",time()-1,"","192.168.1.2");
setcookie("city","",time()-1,"","192.168.1.2");
setcookie("state","",time()-1,"","192.168.1.2");
setcookie("zip_code","",time()-1,"","192.168.1.2");
setcookie("user_name","",time()-1,"","192.168.1.2");
setcookie("password","",time()-1,"","192.168.1.2");
setcookie("email","",time()-1,"","192.168.1.2");
setcookie("last_visit","",time()-1,"","192.168.1.2");
}
}
//Main
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/xhtml1-strict.dtd">
<head></head>
<body>
<?php
{
if(isset($_POST['submit'])) {
switch(strtoupper($_POST['choice'])) {
case "D" :
show_cookie();
break;
case "C" :
cms_delete_cookie();
write_cookie();
break;
case "W" :
write_cookie();
break;
case "E" :
cms_delete_cookie();
break;
case "Q" :
break;
}
} else {
?>
<center>Cookie Manipulator</center>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<?php
if(isset($_COOKIE['last_visit'])) {
?>
<b>D</b>isplay Cookie<br>
<b>C</b>hange Cookie<br>
<b>E</b>rase Cookie<br>
<?php } ?>
<b>W</b>rite Cookie<br>
<b>Q</b>uit<br>
<input type="text" name="choice" size="1" maxlength="1">
<input type="submit" name="submit" value="submit">
<?php
}
 
?>
</form>
<?php
} while($_POST['submit'] != 'Q');
?>
</body>
</html>

--- End code ---

and it still gives me the same problem... Did I do right, Rich? Ernie, say I am dealing with a home computer with very little memory as a server? Don't session variables exact a certain amount of overhead on the server? I could be wrong, though, I do not know.... thanx ahead...
yours,
Jacques P.

ErnieAlex:
So, you want us to help you store "personal" info into cookies, when the entire world says that is bad?
Not going to do that...

Also, the ENTIRE data that you show us is only a few "k" total...  That is nothing as far as memory goes...

Lastly, what could you possibly want to store all that crap into a cookie for???  No reason to every want to do that!  First, cookies are only supposed to be 64 bytes to 128 bytes at the most.  Do some cookie research before asking silly questions.

Store you data in a database, variable or session variables!  No reason ever to use cookies for that much data!

Navigation

[0] Message Index

[#] Next page

Go to full version