FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


Need MAJOR help with cookies! Mmm.... cookies....





polly-gone
My question is, how can I set cookies in the middle of a script. I want to do a login script, and this doesn't appear to be working:

Code:
 if($remember == 'y')
                     {
                           $expire = time()+60*60*24*30;
                     }
                     else
                     {
                           $expire = 0;
                     }
                     
                     //open database connection
                     $connection = mysql_connect($host, $user, $pass) or die('Unable to Connect! Please try again later!');
                     
                     //select database
                     mysql_select_db($db) or die('Unable to select database! Please try again!');
                     
                     //generate and excute query
                     $randomString = randomString(16);
                     $query = "SELECT * FROM marketplace_users WHERE user_name = '$username' AND user_pass = '$password'";
                     
                     $result = mysql_query($query) or die("Error in Query: $query. " . mysql_error());
                     if(mysql_num_rows($result) > 0)
                     {
                           setcookie("LoginInfo",$username,$expire,"/",".marketplace.tsljournal.com");
                           //print results
                           echo '<div align="center">Login Successful!<br>';
                           echo '<a href="index.php">Click here to go back!</a>';
                           echo '<script language="javascript" src="js/timerbar.js"></script></div>';
                     }
                     else
                     {
                           echo '<div align="center">Login Unsucessful...<br>';
                           echo '<a href="login.php">Click here to go back!</a>';
                           echo '</div>';
                     }
                     mysql_close($connection);


It just keeps saying that the header information has already been sent and that it can't modify it.

I am so confused,
-Nick Question Exclamation Question
Stubru Freak
You have to set your cookie before any output.
That's because cookies are sent in the header of an HTTP response, and output is in the response body.
Make sure you don't have any output, not even a new-line, before setting the cookie.
If you need to output something before setting the cookie, you can use Output buffering.
polly-gone
So how would i make this script set it before any output, when this script is in the middle of the page.

-Nick Question Question Question
Stubru Freak
polly-gone wrote:
So how would i make this script set it before any output, when this script is in the middle of the page.

-Nick Question Question Question


Either move it to the beginning, or put
Code:
ob_start();
before any output, and
Code:
ob_end_flush();
after setting your cookie. This will hold the output until you set the cookie.
polly-gone
Thanks, I will try that/

-Nick Idea Idea Idea
rvec
or you could set it with javascript
http://www.quirksmode.org/js/cookies.html
that's a lot of info on how to do that and how it works.

The most important part:
Quote:
document.cookie =
'ppkcookie1=testcookie; expires=Thu, 2 Aug 2001 20:47:11 UTC; path=/'

1. First the name-value pair ('ppkcookie1=testcookie')
2. then a semicolon and a space
3. then the expiry date in the correct format ('expires=Thu, 2 Aug 2001 20:47:11 UTC')
4. again a semicolon and a space
5. then the path (path=/)
Fire Boar
Or you could, instead of using echo [stuff], use $output = [stuff], then stick a echo $output; at the very end of your script. That's my preferred way of doing it, but OB works as well.
imagefree
i have not read all the replies to this topic but i am going to tell you a relevent error. Sometimes when you type
Code:



<?php

session_start();
//etc

?>


It will give error because the first two lines before php starting tag are passed to browser and this will generate error.
rvec
imagefree wrote:
i have not read all the replies to this topic but i am going to tell you a relevent error. Sometimes when you type
Code:



<?php

session_start();
//etc

?>


It will give error because the first two lines before php starting tag are passed to browser and this will generate error.

next time you should read the replies.
Stubru Freak
Fire Boar wrote:
Or you could, instead of using echo [stuff], use $output = [stuff], then stick a echo $output; at the very end of your script. That's my preferred way of doing it, but OB works as well.


That works as well, but OB is easier, especially if you have already typed the rest of your script, and possibly less resource intensive.
kv
Just for your info, the cookie set will not be available in the same page which sets the cookie. It will be available in next http call.
Related topics
Help for sending 2+ cookies in one page.
Major Problem with COmputer. Help Please
[OFFICIAL] How do you study?
I need some major help...
FireFox and Cookies/Sessions
HALF-LIFE 2
Please Help? CGI form mail on my site... only 2 questions!
$_COOKIE and $_SESSION help needed!
Forums in Hosting
How do I install Wordpress?
Cookies help
[Important]Server 2/3 move
Banned from sex
Muslim girl can marry at 15 if she attains puberty: Court
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.