PDA

View Full Version : PHP and Cookies


jtucker135
1-17-02, 11:34 AM
Are cookies working for anyone? I'd really like to see some workin code. None of my local code works on powweb.

Bud
1-17-02, 05:48 PM
I've had no problem using cookies at Powweb.

You can try the following to set a temporary cookie:

#!/usr/bin/perl
use CGI qw(:all);


$cookie=cookie( -name => 'WhatEverYouWant',
-value => "WhatEverYouWant",
-path => '/');

print header(-cookie => $cookie);



~bud :)

Ddr
1-17-02, 07:09 PM
Hi Bud

I am always impressed with your knowledge and answers to the questions. Thank goodness you are around!

I dont know the first thing about cookies. I usually have no trouble finding great tutorials on the web regarding things, but this time, haven't found anything great on cookies. Can you make a suggestion? I am interested in the basic stuff, but also the more advanced features as well.

TYIA

Dale

jtucker135
1-18-02, 10:15 AM
So back to the subject: has anyone been able to successfully implement cookies in PHP?

My site is in PHP and I wanted to avoid having to pass variables to Perl or Javascript.

It looks like the CGI tag, /usr/local/bin/php/, "screws up" (is that tame enough for the site mods?) the implementation of both cookies and session variables in PHP.

sophiespo
1-18-02, 12:04 PM
jtucker135...

ive had no problems with cookies and PHP, except when the #!/usr/local/bin/php line is added to files when it doesnt need to be. That DEFINITELY screws up cookies... Youll know that this is causing your cookies to not work because youll see an error on your page like this:

Warning: Cannot add header information... etc

It basically means the headers already been sent and it cant send the cookie (cookies are sent through headers). When you get this error, youll probably see the #!/usr/local/bin/php on the page too, so just find the file thats causing it, delete the line and try it again.

sophie

jtucker135
1-18-02, 12:20 PM
THANKS! I MUST see this in action!

I've tried deleting this line and then I get the well-known 500 ERROR for not having that line in the code. How did you get yours to work?

Bud
1-18-02, 04:29 PM
hi Ddr, thanks for the compliment :)

The best thing I could tell you would be to grab a CGI scripting book or a Perl book that has at least a chapter or two on CGI.

There are several ways to use cookies, to me it's easiest to use Perl and directly call the cookie subroutines contained in the CGI module.

Wish I could point you to a great tutorial online, but I just haven't seen any I thought were really good.

~bud

jtucker135
1-18-02, 09:40 PM
Thanks, bud. I appreciate your replies.

Yeah, it's easiest for me to use Javascript to handle cookies however I need to do some variable passing to PHP in the same page turn ,so I can't use Perl or Javascript unless I want to convert this whole page to Perl. It would be a shame that for every place on my PHP site that I was using cookies (many) and passing vars(a goodly percent), that I had to convert to Perl. After all, powweb claims to host PHP.


Ok, I got this from a PHP guru but it requires modifying the php.ini file.

--Mark, PHP Guru says ----
All you need to do is change one line in the php.ini file.

alter

output_buffering = Off

to be

output_buffering = On
----------------------------------

This is a site-wide correction for the following functions that don't work for PowWeb's CGI-PHP implementation.

header()
cookies
session variables

Can anyone advise me on how I should proceed in getting this correction implemented?

UnnDunn
1-26-02, 09:55 PM
I am on Mercury.powweb.com. Output buffering is on for my site. I use session variables, and they work just fine.

T-Bone
3-6-03, 10:08 AM
Hi jtuck,

I'm having the same problem and I don't see anyone helping here.

I want to have some more persistant data stored for a user and need to use cookies. I've been testing locally and it works fine.

I have a 'debug.php' include which proves the cookies are set correctly. When I run on the powweb server (lunar.powweb.com) I don't see a cookie stored on my machine despite the fact that I am setting an expiration of 10 days.

The only cookie variable I see from the powweb server is PHPSESSID. Here's the result of my debug displays on the powweb server:


Get variables:
debug = y


Cookies variables:
PHPSESSID = 992b35a09e1bbee8751a915e0e991e73


Session variables:
loginStatus = good
leagueID = 1
PlayerName = Tim Harris
LeagueName = Osram Sylvania Golf League
isAdmin = 0
Player = 17


Here's the display locally:

'Get' variables:
debug = y


Cookies variables:
PHPSESSID = 1f8165e94b8f258fe9dfc9f02066d437
LeagueYear = 2003
loginID = test
pWord = user
leagueID = 1
LeagueName = Osram Sylvania Golf League
Player = 17
Team = 6
PlayerName = Tim Harris
email = timothy.harris@sylvania.com


Session variables:
loginStatus = good
...


Any other thoughts?

I've displayed some of the ini settings and they seem close to my local settings.

Regards,
T-Bone

Pig
3-6-03, 10:37 AM
I have been using cookies just fine, without modifying output buffering.

Can you display the code you are using to set the cookie? What browser are you using?

T-Bone
3-6-03, 01:41 PM
I'm using IE 6.0 and here's the code to set the cookie.
As I stated, it works fine locally. :(

$x = validateUser($db,trim($_POST["loginID"]),trim($_POST["pword"]));
if ($x){
$expireTime = 10*24*60*60;
echo("Expire Time: ".$expireTime."<br>");
setcookie("loginID", $_POST["loginID"],$expireTime);
setcookie("isAdmin", $_POST["isAdmin"],$expireTime);
setcookie("pWord", $_POST["pword"],$expireTime);
setcookie("leagueID", $_SESSION['leagueID'],$expireTime);
setcookie("LeagueName", $_SESSION['LeagueName'],$expireTime);
setcookie("Player", $_SESSION['Player'],$expireTime);
setcookie("Team", $_SESSION['Team'],$expireTime);
setcookie("LeagueYear", date("Y"),$expireTime);
setcookie("PlayerName", $_SESSION['PlayerName'],$expireTime);
setcookie("email", $_SESSION['email'],$expireTime);
$_SESSION['loginStatus'] = "good";
header("Location: http://".$_SERVER['HTTP_HOST']
.dirname($_SERVER['PHP_SELF'])
."main.php");
} else {
$msgText .= "Invalid login information ";
}

T-Bone
3-7-03, 01:10 PM
In the code in my previous post, I noticed I didn't add 10 days to the current time. I corrected that and it worked.

$expireTime = time() + (10*24*60*60);

jtuck, make sure you give your cookie and expiration time otherwise I think the server treats it like a session type of cookie not a persistant cookie.

hth,
:D