/    Sign up×
Community /Pin to ProfileBookmark

User Cookies Problem

Hello there,

I want to create a simple user Cookie which if a user enters a name in the form and submits it it will show the username he entered..

Else it will show “Welcome Guest”.

Here’s what I have.

The Form:

[code=php]echo “<form action=’php.php’ method=’post’>
<input type=’text’ name=’userlog’ />
<input type=’submit’ name=’login’ value=’Log in’ />
</form>”;
[/code]

The Cookie:

[code=php]$expire = time()+60*60*24*30*365;

if (isset($_POST[‘login’])) {
$user = $_POST[‘userlog’];
setcookie (‘logged’, $user, $expire);
if (isset($_COOKIE[‘logged’])) {
echo “Welcome : ” . $_COOKIE[‘logged’] ;
}
}
else {
echo “Welcome : Guest” ;
}[/code]

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@SyCoDec 04.2008 — Is this just an example because you want to learn how to set a cookie or is this the actual code you plan to use? You're testing for $_POST['login'] then assuming $_POST['userlog'] exists.

$expire is 60 seconds * 60 minutes * 24 hours * 30 * 365, that's an expire time of 30 years!
[CODE]
60 seconds in a minute
60 3600 seconds in an hour
24 86400 seconds in a day
365 31536000 seconds in a year
30 946080000 seconds in 30 years[/CODE]


Have a look at the manual page setcookie()


[code=php]
$expire = time()+60*60*24*365;
if (isset($_POST['login']) && isset($_POST['userlog'])) {
setcookie ('logged',$_POST['userlog'], $expire);
if (isset($_COOKIE['logged'])) {
echo "Welcome : " . $_COOKIE['logged'] ;
}
}
else {
echo "Welcome : Guest" ;
}
[/code]



The value in the cookie is set when the page containing the code is set. It's not available to the script until the page is refreshed.

This can be accomplished very simply with a $_SESSION variable. Set like any other variable eg.

$_SESSION['userlog']=$_POST['userlog'];

So Your user can have a message personalized on ever page and a welcome message show only after they log in like this.

[code=php]if (isset($_POST['login']) && isset($_POST['userlog'])){
//add additional user login validation here
$_SESSION['userlog']=$_POST['userlog'];
}else{
//handle login failure
}

if (isset($_SESSION['userlog'])) {
//logged in check independent of login logic
echo "Logged in as : " . $_SESSION['userlog'] ;
}else{
echo "Welcome : Guest" ;
}
[/code]


Now a user will get the 'Logged in as Bob' message on every page the code is included on. regardless of whether they just logged in.

Logins are usually handled by SESSIONs and 'keep me logged in' (more than this session) is usually handled with COOKIEs
Copy linkTweet thisAlerts:
@Lo0kBeHiNdUauthorDec 04.2008 — About the expire date..? yes excuse me I made a mistake there.

Now about the cookie.

It works fine though

wenn I just close the tab and then visit the page again it shows

"Welcome Guest"

"Logged in as: Username(the previous session username)"

What can I do here to prevent that?
Copy linkTweet thisAlerts:
@SyCoDec 05.2008 — In both examples the two messages are either side of an if() clause it's not possible to show both unless you've copies the code and left old code or are mixing the sessions and cookies code. The 2 code examples I posted are independent and not to be used together. Either the first or the second. I think you are looking for the sessions solution.

Cookies could be used for a welcome back message or to auto login a user, not to show a welcome message after login without another page redirect.
×

Success!

Help @Lo0kBeHiNdU spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.15,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...