Forum Moderators: coopster

Message Too Old, No Replies

Cookie help please

         

Acternaweb

7:19 pm on Apr 25, 2005 (gmt 0)

10+ Year Member



I am trying to create a simple cookie (so I thought) that after my user enter's his/her name, they would be greated by their name on other pages within my site.

However I am not having any luck. Please help.

mcibor

9:44 pm on Apr 25, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



<?php
if(isset($_POST["user"]))
{
$user = $_POST["user"];
setcookie("user", $user, time() + 3600*24*12);//valid 1 year
}

if(!isset($_COOKIE["user"]))
{
echo "<form action=\"".$_SERVER['PHP_SELF']."\" method=\"POST\">";
?>
Please input your name: <input type="text" name="user">
<?php
}
else
{
$user = $COOKIE["user"];
//your normal code.
}?>


This way on the first appearance the user writes his name and then it is remembered for 1 year.
If you want to refresh remembering the cookie (to make it last longer if user visits the page, then setcookie after reading it. If you want to make a session cookie (valid till the browser is open) then put the time to 0.
best regards
Michal Cibor

Acternaweb

3:59 pm on Apr 26, 2005 (gmt 0)

10+ Year Member



hey mcibor

thanks for the help, but something is not working right. Is it suppoed to be just one php statement or two?

mcibor

5:49 pm on Apr 26, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



There was a mistake in second if. It should be:
<?php
if(isset($_POST["user"]))
{
$user = $_POST["user"];
setcookie("user", $user, time() + 3600*24*12);//valid 1 year
}

if((!isset($_COOKIE["user"]) &#166;&#166; empty($_COOKIE["user"])) &&!$user)
{
echo "<form action=\"".$_SERVER['PHP_SELF']."\" method=\"POST\">";
?>
Please input your name: <input type="text" name="user">
<input type="submit">
<?php
}
else
{
if(!$user) $user = $COOKIE["user"];
echo "Welcome user ".$user.".";
//your normal code.
}?>


One or two <?php is irrelevant - you may have as many as you wish, unless you don't open them without closing.
Hope this works
Michal Cibor

Acternaweb

8:06 pm on Apr 26, 2005 (gmt 0)

10+ Year Member



Hi again,

Not having much luck. I tried doing this myself but have no idea what the error means (Warning: Cannot modify header information).

my php script is:

#!/usr/local/bin/php --
<?php
// Set a cookie that expires in one hour
setcookie("userName", $name, time()+3600);
?>

On the login page I have

<form id="myform" name="myform" method="post" action="http://nova.umuc.edu/cgi-bin/cgiwrap/em680a03/name.php">
<font face="tahoma" color="#0054A9" font size="2pt">
&nbsp;&nbsp;&nbsp; <input type="text" size="20" name="name" value="enter name"><P>
&nbsp;&nbsp;&nbsp;<input type="submit" value="go"> </form>

On the page that I want to see the "cookie" I have
<?php
if (isset($userName))
print "Welcome " . $userName . "<br>";
else
print "You are not logged in <br>";
?>

What am I doing wrong, really starting to confuse the heck out of myself. Appreciate your help

mcibor

8:11 pm on Apr 26, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



the error means that you are outputting something before setting the cookie. The setcookie has to be done before everything else.

BTW you aren't allowed to post here urls, like you did in action.

For me to correct the code i need to know what do you write before setcookie(), (how do you get $name)

Michal Cibor

Acternaweb

11:12 pm on Apr 26, 2005 (gmt 0)

10+ Year Member



mcibor

I am getting very confused. Let me start over and explain what I am trying to do. On my home page I will have a form where the user will enter their name. From that page on, each page will say "hello name" (for example).

<?php
//check to see if $_SESSION['your name'] contains anything
if (!empty($_SESSION['your name']))
{
echo "I already know your name," , $_SESSION['your name'];
}
else
{
if (empty($_POST['submit']))
{ echo "<form name=myform method=post action=$PHP_SELF>;
<input type=test name=first_name>first name<BR>
<input type=test name=last_name>last name<BR>
<input type=submit name=submit value=submit>
</form>";

}
else
{

$_SESSION['your name'] = "$first_name $last_name";
echo "Thank you, {$_SESSION ['your name']}";
}
}
?>

mcibor

8:13 pm on Apr 27, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This is the corrected code:
<?php
session_start();
if (!empty($_SESSION['your name']))//check to see if $_SESSION['your name'] contains anything
{
echo "I already know your name, " . $_SESSION['your name'];
}
else
{
if (empty($_POST['submit']))
{ echo "<form name=myform method=post action=$PHP_SELF>;
<input type=text name=first_name>first name<BR>
<input type=text name=last_name>last name<BR>

<input type=submit name=submit value=submit>
</form>";

}
else
{
$_SESSION['your name'] = $_POST['first_name']." ".$_POST['last_name'];
echo "Thank you, {$_SESSION ['your name']}";//why in brackets?
}
}
?>


Never use global variables on
Michal Cibor

Acternaweb

11:01 pm on Apr 27, 2005 (gmt 0)

10+ Year Member



HI

I did not see anything when I used the code, it was a solid white page. What did I do wrong?

mcibor

8:21 pm on Apr 28, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I copied and pasted the above code and it worked. There's an unnecessary colon on 10 line, but it doesn't matter. Try to see the source code (right click with mouse and show the source). Also turn error reporting to all, just for the sake of programming - there might be something missing.

Good luck!
Michal Cibor

Acternaweb

11:02 pm on Apr 28, 2005 (gmt 0)

10+ Year Member



mcibor

I keep getting

Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at /class/em680a/03/www/cgi-bin/name.php:3) in /class/em680a/03/www/cgi-bin/name.php on line 6

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /class/em680a/03/www/cgi-bin/name.php:3) in /class/em680a/03/www/cgi-bin/name.php on line 6

jatar_k

11:07 pm on Apr 28, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



and what is on the sixth line in name.php?

Acternaweb

12:00 am on Apr 29, 2005 (gmt 0)

10+ Year Member



Here are the top lines, the last is line 6
I need the first one or the script doesn't work.

#!/usr/local/bin/php --
<?php
session_start();
if (!empty($_SESSION['your_name'])) //check to see if $_SESSION['your_name'] contains anything
{
echo "I already know your name, " . $_SESSION['your_name'];

mcibor

8:28 pm on Apr 29, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



But it's that first line that makes the setting of cookies impossible.

Why do you have to put btw? I never heard of such thing.

What happens if you cut that line out? (#!/usr/local/bin/php -- )?

Write, then we may be able to help. There's a way to process a php even if it's without that piece of code.

And headers sent means that you sent to the user's computer something.

Best regards
Michal Cibor

Acternaweb

8:45 pm on Apr 29, 2005 (gmt 0)

10+ Year Member



I got it to work, the reason is I had a return between that first line and PHP.

Now how do I get the cookie to show up on another page?

Thanks so much.

mcibor

8:52 pm on Apr 29, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Use the same code! That's what's so great about the session.
You can put the form into a redirect, or just use the form action to self. It's up to you.

However there are some problems if you redirect and still want the session to last.

Good luck!
Glad this at least worked out
Michal Cibor