Jump to content

Cookies!


roper22

Recommended Posts

Hi everyone. I'm kinda new to PHP, but I feel as though I am learning quite quickly. I was attempting to make a login, but I hit a roadblock in this...

 

--------------------------------------------------------------

<?php

setcookie("cookie", "Hello.", time()+3200);

?>

--------------------------------------------------------------

 

 

 

That code works.

 

 

 

 

 

 

 

 

---------------------------------------------------------

<?php

include 'connect.php';

 

$password = $_POST['password'];

$username = $_POST['username'];

 

$sql = "SELECT * FROM users

WHERE username = '".$username."'

AND password = '".md5($password)."'";

 

$result = mysql_query($sql);

$num = mysql_num_rows($result);

 

if ($num > 0) {

//USER AND PASS ARE CORRECT

$id = mysql_fetch_array($result);

echo "s";

setcookie("cookie", "Hello.", time()+3200);

} else {

echo "f"; }

?>

---------------------------------------------------------

 

 

 

But after taking that EXACT line, and putting it in my login script, it doesn't work.

 

Help appreciated! Thanks tons guys.

 

 

The first code can be seen active on www.trportfolio.net/cookie_test.php

 

The second can be seen if you register at www.trportfolio.net/register.php and then login at www.trportfolio.net/login.php

Link to comment
Share on other sites

Cookie information is sent with headers and you can't echo anything to the browser without sending all of the headers.  You don't have error reporting turned on, but if you did you would get a message about headers already sent, unable to blah blah blah.

 

Either queue up all of your output into a variable and echo it a single time at the end, turn on output buffering, or set your cookie before you echo anything.

Link to comment
Share on other sites

Cookie information is sent with headers and you can't echo anything to the browser without sending all of the headers.

 

Like I said, i'm new to PHP, and to be honest, I have no idea what that means. I took out any "echo" scripts that I had, yet the cookies still don't work. Maybe I misunderstood you when you said "Set your cookie before you echo anything."

 

My code now looks like this...

 

<?php
include 'connect.php';

$time = time();
$password = $_POST['password'];
$username = $_POST['username'];

$sql = "SELECT * FROM users
WHERE username = '".$username."'
AND password = '".md5($password)."'";

$result = mysql_query($sql);
$num = mysql_num_rows($result);

if ($num > 0) {
//USER AND PASS ARE CORRECT
$id = mysql_fetch_array($result);
setcookie("cookie", "Hello.", time()+3200);
header("Location: login_successful.php");
} else {
header("Location: login_fail.php");
}
?>

 

Since we were on the topic of headers, I figured i'd tackle another problem and ask for a bit of help about why this isn't working. Please realize that I am not fully aware of all these 'exceptions' yet. I am actually learning a lot of my codes from tutorials, but I am tweaking it to how i'd like it, as I attempt to learn in the process. I apologize for the newbie questions, and your help is much appreciated :D

 

(Sorry for being a nub roopurt, lol.)

Link to comment
Share on other sites

You should call exit() after making a call to header where you set the location.

 

Headers are basically "hidden" information that is sent between the browser and web server; it's hidden in the sense that you have to make extra effort to view it in most browsers.  Header information gives the browser clues on what to expect, such as what type of file the server is about to send or if it should set a cookie or not.  The way the HTTP protocol works headers have to be sent before any other form of output, such as the actual HTML.  The moment your script spits out any output, even a white space, you are no longer allowed to set any header information.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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