/    Sign up×
Community /Pin to ProfileBookmark

Cookie Problems

I’m having a hard time with cookies i have a basic test to see if cookies are enabled and if they are to create cookies if not then to create a session.

now the test to see if the cookies are enabled works fine how ever i can’t create the cookies

what happens is when a user login it checks to see if there registered and if the password that was inputted is correct to the one they registered with basic authentication if they are who they say they are then it creates the cookie with the userID all of this is done before any other header information is set however it doesn’t create the cookies

[code=php]
if(!isset($red) && !isset($_COOKIE[‘test’])) {
setcookie(“test”,”true”,0,”/”,”.purelyme.co.cc”);
$red=1;
header(‘location:’.$_SERVER[‘PHP_SELF’].”);
}

if (isset($_POST[‘login’])) {
$action = “Signing in Please Wait. <br/> <img src=’images/loader.gif’ />”;
if(isset($_POST[’email’])){
$sql = “SELECT * FROM `Users` WHERE `Email` ='”.$_POST[’email’].”‘”;
$results = mysql_query($sql, $conn) or trigger_error(“SQL”, E_USER_ERROR);
$newArray = mysql_fetch_array($results);
if ($newArray[‘Activated’] != “N”) {
if ($newArray[‘Password’] == $_POST[‘password’]) {

if(isset($_COOKIE[‘test’])){
setcookie(“user”,$newArray[‘ID’],time()+3600*24*30,”/”,”.purelyme.co.cc”);
setcookie(“type”,$newArray[‘ProfileType’],time()+3600*24*30,”/”,”.purelyme.co.cc”);
if(isset($_POST[‘remember’])){
setcookie(“remember”,”checked”,time()+3600*24*30,”/”,”.purelyme.co.cc”);
setcookie(“pass”,$newArray[‘Password’],time()+3600*24*30,”/”,”.purelyme.co.cc”);
}
}else{
session_start();
$_SESSION[‘id’] = $newArray[‘ID’];
$_SESSION[‘ProfileType’] = $newArray[‘ProfileType’];
}
}else{
$response = “Wrong Email / Password.”;
$success = “N”;
}
}else{
$response = “Your account has not been Activated yet Please Check you Email.”;
$activated = “N”;
}
}
}
[/code]

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@dk_zero-coolMay 25.2010 — First of, why use so many cookies?

You could put everything in an array and save it in one single cookie using serialize().

Second, it's not smart saving passwords in cookies.

The cookie content is sent unsecure on every request.

Why not just use a session to store the user information?

Third, your problem is your cookie test.

The user submits the form, it hits the cookie test which redirects the user.

When you redirect, the form data is not sent along. So when the user returns to the page again efter the cookie test, the form data is lost. You should set the test cookie the first time the HTML Form is loaded, before any form submits.
Copy linkTweet thisAlerts:
@YokiestauthorMay 26.2010 — First of, why use so many cookies?

You could put everything in an array and save it in one single cookie using serialize().

Second, it's not smart saving passwords in cookies.

The cookie content is sent unsecure on every request.

Why not just use a session to store the user information?

Third, your problem is your cookie test.

The user submits the form, it hits the cookie test which redirects the user.

When you redirect, the form data is not sent along. So when the user returns to the page again efter the cookie test, the form data is lost. You should set the test cookie the first time the HTML Form is loaded, before any form submits.[/QUOTE]


the password i understand i'm new to cookie authentication methods also when you first go to the page the cookie test is created before you submit the form
[code=php]
if(!isset($red) && !isset($_COOKIE['test'])) {

setcookie("test","true",0,"/",".purelyme.co.cc");

$red=1;
header('location:'.$_SERVER['PHP_SELF'].'');

}
[/code]


and also if you noticed in my login script it doesn't create the test cookie just see's if it is set
Copy linkTweet thisAlerts:
@dk_zero-coolMay 26.2010 — The script checks to see if the cookie exist. If not, it is created and the redirect header is created which will redirect the client. That's what that code does.

Now I don't see a form in that code, so when this code is executed, I don't know.

How about including everything in that file?
Copy linkTweet thisAlerts:
@YokiestauthorMay 26.2010 — Here is the whole login code from start to finish minus the log in form
[code=php]
if(!isset($red) && !isset($_COOKIE['test'])) {

setcookie("test","true",0,"/",".purelyme.co.cc");

$red=1;
header('location:'.$_SERVER['PHP_SELF'].'');

}

if (isset($_POST['login'])) {
$action = "Signing in Please Wait. <br/> <img src='images/loader.gif' />";
if(isset($_POST['email'])){
$sql = "SELECT * FROM Users WHERE Email ='".$_POST['email']."'";
$results = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR);
$newArray = mysql_fetch_array($results);
if ($newArray['Activated'] != "N") {
if ($newArray['Password'] == $_POST['password']) {

if(isset($_COOKIE['test'])){
setcookie("user",$newArray['ID'],time()+3600*24*30,"/",".purelyme.co.cc");
setcookie("type",$newArray['ProfileType'],time()+3600*24*30,"/",".purelyme.co.cc");
if(isset($_POST['remember'])){
setcookie("remember","checked",time()+3600*24*30,"/",".purelyme.co.cc");
setcookie("pass",$newArray['Password'],time()+3600*24*30,"/",".purelyme.co.cc");
}
}else{
session_start();
$_SESSION['id'] = $newArray['ID'];
$_SESSION['ProfileType'] = $newArray['ProfileType'];
}
}else{
$response = "Wrong Email / Password.";
$success = "N";

}
}else{
$response = "Your account has not been Activated yet Please Check you Email.";
$activated = "N";
}
}
}


[/code]
×

Success!

Help @Yokiest 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.2,
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: @Yussuf4331,
tipped: article
amount: 1000 SATS,

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

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...