sasori Posted September 21, 2008 Share Posted September 21, 2008 hello, i just bumped into setting a cookie lesson i tried to run this to my browser <?php $value = "something from somewhere"; setcookie("test cookie",$value,time()+3600,"/webapp/","webapp"); ?> and the error says, Warning: Cookie names can not contain any of the folllowing '=,; \t\r\n\013\014' (Test Cookie) in C:\wamp\www\webapp\setcookie.php on line 3 I don't understand it Quote Link to comment Share on other sites More sharing options...
JasonLewis Posted September 21, 2008 Share Posted September 21, 2008 Take out the space, use underscores (_) when separating words. Quote Link to comment Share on other sites More sharing options...
sasori Posted September 21, 2008 Author Share Posted September 21, 2008 Take out the space, use underscores (_) when separating words. on what part? you mean like this? setcookie("test_cookie", $value,time(),path,domain) is it ? Quote Link to comment Share on other sites More sharing options...
JasonLewis Posted September 21, 2008 Share Posted September 21, 2008 Yeah, give that a go. I've never used spaces in my names, and you hardly do in PHP. So I've never personally encountered the error. Quote Link to comment Share on other sites More sharing options...
sasori Posted September 21, 2008 Author Share Posted September 21, 2008 im encountering the last error in my script i don't know how to configure it first,here's the db composition ,uid,first_name,user_name,, ,1,peter,spiderman,,, ,2,henry,beast,,, now here's my log-in form script //test1.php <?php if(isset($message)) { echo $message; } echo "<html>\n"; echo "<body>\n"; echo "<form action='$_SERVER[php_SELF]' method='POST' >\n"; echo "<label for ='user_name'>User id</label>\n"; echo "<input type='text' name='user_name' size='20' id='user_name' value='$user_name' />"; echo "<label for ='password'>Password</label>\n"; echo "<input type='text' name='password' size='20' value='$password' />"; echo "<input type='hidden' name='sent' value='yes' />"; echo "<input type='submit' value='login' />"; echo "</form>\n"; echo "</body>\n"; echo "</html>\n"; ?> here's the logic script //logincookie.php <?php if(isset($_POST['sent']) && $_POST['sent'] == "yes") { foreach($_POST as $field => $value) { if(empty($value)) { $blank_array[$field] = $value; } else { $good_data[$field] = strip_tags(trim($value)); } }//end foreach if(sizeof($blank_array) > 0) { $message = "fill up both forms"; extract($blank_array); extract($good_data); include("test1.php"); exit(); } include("db.php"); $cxn = mysqli_connect($host,$user,$pwd,$db) or die ("can't connect"); $query = "SELECT first_name FROM users2 WHERE user_name='$_POST[user_name]' AND pwd=md5('$_POST[password]')"; $result = mysqli_query($cxn,$query) or die ("can't execute query"); $n_row = mysqli_num_rows($result); if($n_row < 1) { $message = "user not found"; extract($_POST); include("test1.php"); exit(); } else { $row = mysqli_fetch_assoc(result); setcookie("first_name",$row['first_name']); setcookie("auth","yes"); header("Location: http://localhost/webapp/test2.php"); } } else { $user_name = " "; $password = " "; include("test1.php"); } ?> and here's the greeting page script //test2.php <?php if($_COOKIE['auth'] != "yes") { header("Location: http://localhost/webapp/logincookie.php"); exit(); } echo "welcome to php {$_COOKIE['first_name']}"; echo "<form action='logincookie.php' method='get'>"; echo "<input type='submit' value='back' />"; echo "</form>\n"; ?> and when i tried to input a db existing username in the form the output only says, Welcome to PHP (it doesn't include the first name that should be welcomed ) ??? Quote Link to comment Share on other sites More sharing options...
Andy17 Posted September 21, 2008 Share Posted September 21, 2008 Well, I coded a "remember me" function yesterday (first time I ever experienced with cookies), so I'm not sure that the following is your problem. However, I noticed that you do not set an expire date for your cookies. Logically I'd guess that this just means that it's deleted when you close your browser. So, if this is correct, it wouldn't be able to echo the name stored in the cookie, since it would be deleted if you have closed your browser since your last visit. This might not be your problem (depends if you test it after you have closed your browser I believe), but try this: <?php // This just makes the cookie expire in 10 years from the day it's set; you can just edit the number 10 to whichever number of years you want $expire = 60 * 60 * 24 * 365 * 10 + time(); setcookie("first_name", $row['first_name'], $expire); setcookie("auth","yes", $expire); ?> Again, I'm new to this so this might not be your problem. Also, I noticed that you are using a whole lot of echos to post your html code. Why don't you just do like this instead? <?php //test1.php if(isset($message)) { echo $message; } ?> <html> <body> <form action='<?php echo $_SERVER[php_SELF]; ?>' method='POST'><br> <label for ='user_name'>User id</label><br> <input type='text' name='user_name' size='20' id='user_name' value='<?php echo $user_name; ?>' /><br><br> <label for ='password'>Password</label><br> <input type='text' name='password' size='20' value='<?php echo $password; ?>' /><br> <input type='hidden' name='sent' value='yes' /> <input type='submit' value='login' /> </form> </body> </html> You know, you can have html code inside PHP, like this for example: <?php if (1 == 1) { ?> This text is written in html<br>HTML tags work too! <?php } else { ?> More HTML code here if you want <?php } ?> That was a little off-topic, but it's a good way to clean up your code a little. Quote Link to comment Share on other sites More sharing options...
sasori Posted September 21, 2008 Author Share Posted September 21, 2008 to Mr. Andy17 and ProjectFear .. thanks for the tips.. it helps alawt Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.