Loop problems...


#1

hello,
I am having a problem looping with the do…while loop. Here is the code for cms_cookie1.php:
[php]<?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."
";
echo $middle_name."
";
echo $last_name."
";
echo $address."
";
echo $city."
";
echo $state."
";
echo $zip_code."
";
echo $phone."
";
echo $user_name."
";
echo $password."
";
echo $email."
";
echo $last_visit."
";
}
function write_cookie()
{
?>
<form action="<?php echo "fill_cookie.php"; ?>" method=“POST”>
First Name:

Middle Name:

Last Name:

Address:

City:

State:

Zipcode:

User Name:

Password:

Email:

Phone:



<?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 ?> <?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 { ?> Cookie Manipulator <?php if(isset($_COOKIE['last_visit'])) { ?> Display Cookie
Change Cookie
Erase Cookie
<?php } ?> Write Cookie
Quit
<?php }

?>

<?php } while($_POST['submit'] != 'Q'); ?> [/php] and here is the code for fill_cookie.php: [php]<?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"); ?> [/php] 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: [/code], until I halt it with ctrl-c. I am baffled!!! Thanx ahead, Jacques P.

#2

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


#3

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…


#4

OK dudes,
I put 2 if(isset…) statements in two of the functions, as follows:
[php]<?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’]."
";
echo $_COOKIE[‘middle_name’]."
";
echo $_COOKIE[‘last_name’]."
";
echo $_COOKIE[‘address’]."
";
echo $_COOKIE[‘city’]."
";
echo $_COOKIE[‘state’]."
";
echo $_COOKIE[‘zip_code’]."
";
echo $_COOKIE[‘phone’]."
";
echo $_COOKIE[‘user_name’]."
";
echo $_COOKIE[‘password’]."
";
echo $_COOKIE[‘email’]."
";
echo $_COOKIE[‘last_visit’]."
";
}
}
function write_cookie()
{
?>
<form action="<?php echo "fill_cookie.php"; ?>" method=“POST”>
First Name:

Middle Name:

Last Name:

Address:

City:

State:

Zipcode:

User Name:

Password:

Email:

Phone:



<?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 ?> <?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 { ?> Cookie Manipulator <?php if(isset($_COOKIE['last_visit'])) { ?> Display Cookie
Change Cookie
Erase Cookie
<?php } ?> Write Cookie
Quit
<?php }

?>

<?php } while($_POST['submit'] != 'Q'); ?> [/php] 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.

#5

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!


#6

You could also just do it 1 in cookie, client info for example.


#7

Okay, this is my opinion on this cookie issue. Then, I will explain some fixes for it…

First, a cookie is supposed to be to track a client or customer by saving a unique ID created for that person. It is a small number saved on that computer for use as an index into a database where the server can pull all of his/her data such as what was indicated by “dementedgimp”. Then, all of this data would be available for the page to use. This is secure and not a big problem except that it does track the user. It basically can include a timestamp which can be save in the database for the server to track user usage records.

Now, the data that was listed in the code is PRIVATE data. It should never be stored locally. It is a small amount of data and ANY computer in the world could hold this small amount in memory or a text file or database. Not much to deal with. So, saving it in a cookie is just not correct in my opinion.

So, now with that said…

One of the biggest issues of is it must be handled BEFORE any headers are posted to the browser. Cookies need to be set in the header. This means they must be sent before any HTML is set to the page, or they will not work. So, in PHP, you must make sure the code is before the sections of the page. It will not work half way down your HTML page… Next, in dementedgimp’s sample, he is setting about a dozen cookies, not just one cookie.

So, the easiest way to fix this would be to have a PHP script BEFORE your tags before HTML is posted to the browser. In this section, you would read all your cookies putting them into an array. Once these are turned into PHP variables or an array of them, they would be available thru the entire page for use as you need them. Of course, if you need to rewrite them, you must do so inside the header, so it would have to be done by calling another PHP page. Again, not what I suggest.

Well, That might help fixing the errors you are receiving. My guess is that you are note handling them before the HTML body… Not sure, but, after you have retrieved them once, I think they are usable inside your HTML with PHP. Never tested that myself.

Hope this works… Good luck…