Jump to content

Question about Cookie?


bulgaria_mitko

Recommended Posts

i have this problem i dont know if it a problem or it should be like that, im using cookie to display a name and in index.php the person write her/his name and press on submit then the site go to page call user.php and it needs to be reloaded before the name is display ([b]is that how it supposed to be?[/b]) so far so good and here comes the problem when i person want to delete the cookie and re-enter other name instead of a name i get a number for example 1165924546 (example "Hello, 1165924546!") i will write you the code im using for index.php and user.php

index.php code is:

<form action="user.php" method="post">
<input type="text" name="username">
<input type="submit" value="изпрати">
</form>

user.php:

<?php
ob_start()
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=koi8-r" />
<title>Потребителска информация</title>
</head>

<body>

<?php
if(!isset($_COOKIE['user']))
{
    setcookie ("user", $_POST['username']);
    header('user.php');
}
else
{
    echo 'Здравейте, '.$_COOKIE['user'];
}
?>

<?php
echo '!<br>Кликнете <a href="deletecookie.php">тук</a>, за да смените името, което посочихте или <a href="content.php">тук</a> за да продължите!'
?>


</body>
</html>

ps: some of the text is in bulgarian dont worrie about it :) and i have to say that i dont use any database (for now) i mean i dont want to use it and is it possible to do it without database? thank you very much!!!
Link to comment
https://forums.phpfreaks.com/topic/30345-question-about-cookie/
Share on other sites

You have to delete a cookie with the same parameters you set it with, so try this as the delete cookie page...

[code]<?php
setcookie ("user", '', time()-3600);
header("Location: http://localhost/phpuni/index.php"); /* Redirect browser */
exit;
?>[/code]

The number you were getting was because you were setting [code=php:0]time()-3600[/code] as the value of the cookie, not the expiry time.

Also, please use [b][nobbc][code][/code][/nobbc][/b] tags when posting code.

Regards
Huggie
Link to comment
https://forums.phpfreaks.com/topic/30345-question-about-cookie/#findComment-139615
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.