Forum Moderators: coopster
<?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.
}?>
<?php
if(isset($_POST["user"]))
{
$user = $_POST["user"];
setcookie("user", $user, time() + 3600*24*12);//valid 1 year
}if((!isset($_COOKIE["user"]) ¦¦ 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.
}?>
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">
<input type="text" size="20" name="name" value="enter name"><P>
<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
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
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']}";
}
}
?>
<?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?
}
}
?>
Good luck!
Michal Cibor
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
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