Cookie question

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Ahms
Forum Newbie
Posts: 7
Joined: Mon Dec 01, 2003 10:21 am

Cookie question

Post by Ahms »

Quick question. Just moved to a new host, this script worked on my old server but now it's not:

Code: Select all

<?php

mysql_connect('*****', '*****, '*****');
mysql_select_db('*****');

$username = $_POST&#1111;'login'];
$password = $_POST&#1111;'password'];

$query = mysql_query("SELECT * FROM users WHERE username = '$username' AND password = '$password'");

if (mysql_num_rows($query) > 0) &#123;
	setcookie ('guildname', $username, "31104000", "/");
	include 'welcome.php';
&#125;
else &#123;
	include 'failed.php';
&#125;

mysql_close();
?>

Basically what's happening is that the cookie isn't setting. It worked fine on my old server but not now, am I doing something wrong? The Apache and PHP versions are the same, the only difference is that this new host uses mySQL 4x while the old one used 3x, I don't know if that makes a difference or not. Please let me know if I'm too vague and you need more information, thanks!
User avatar
xisle
Forum Contributor
Posts: 249
Joined: Wed Jun 25, 2003 1:53 pm

Post by xisle »

have you tried adding the domain to your cookie?

Code: Select all

$domain=explode(".", $HTTP_HOST);     		
$domain&#1111;0]="";     		
$domain=implode(".", $domain);
					  
// set 365 day expiration..86400 per day				
$die = $now +31536000;	

 if(!$c=setcookie("guildname", $username, $die, "/", "$domain")) &#123;    
    print"whatever";
    exit;
 &#125;
Ahms
Forum Newbie
Posts: 7
Joined: Mon Dec 01, 2003 10:21 am

Post by Ahms »

Set the domain but nothing happened. I check temp files and find that no cookie at all is setting :(
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post by m3mn0n »

Not sure about the cookies, but you should encrypt the passwords in the database. :wink:
Ahms
Forum Newbie
Posts: 7
Joined: Mon Dec 01, 2003 10:21 am

Post by Ahms »

Will do lol

I got it to work btw! I just changed it to this:

Code: Select all

setcookie ("guildname", $username, time()+3600, "/");
I'm glad it works now but WHY is it working now?
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post by m3mn0n »

Thats the proper syntax as described here: [php_man]setcookie[/php_man]

Why did it set time to a static value before?
Ahms
Forum Newbie
Posts: 7
Joined: Mon Dec 01, 2003 10:21 am

Post by Ahms »

I had it like this before on my old server:

Code: Select all

setcookie ('guildname', $username, "31104000", "/");
There it actually worked fine. But I did move to a new server, is just really picky or something?
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post by m3mn0n »

Most likely a newer server. Or just a differently configured server. That's why it's best to code keeping in mind that it should work on all platforms and with all (or atleast most) setting types.
Post Reply