Jump to content

[SOLVED] setcookie problem


sasori

Recommended Posts

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

 

 

 

Link to comment
Share on other sites

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 ) :( ???

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.