Jump to content

Archived

This topic is now archived and is closed to further replies.

oMIKEo

Cookies issue...

Recommended Posts

Hi,

Can anyone tell me why this code isnt working, the login script creates a cookie, then redirects you to a profile page, that page first checks to see if the cookie exists otherwise kicks you to a login page.

I added print_r($_COOKIE); onto the profile page to see what cookies are being passed to that page, it comes back with none :S

[code]<?php
include "config.php";

if( (!$username) or (!$password) )
{ header("Location:$HTTP_REFERER"); exit(); }

$conn=@mysql_connect("$db_host","$db_username","$db_password") or die("Could not connect");
$rs = @mysql_select_db($db_main,$conn) or die("Could not select database");

$sql="select * from bn_profile where band_email=\"$username\" and password=\"$password\"";
$rs=mysql_query($sql,$conn) or die("Could not execute query" . mysql_error());
$num12 = mysql_num_rows($rs);
if($num12!=0)
{
setcookie("UN", $username, time()+3600, "/", ".breakingtuneshosting.com", 0);
header("Location:profile.php"); exit();
}
else
{ header("Location:$HTTP_REFERER"); exit(); }
?>[/code]
Also if i echo $num12; i get a value of 1 so it should be creating the cookie.

Any ideas?

Share this post


Link to post
Share on other sites
[code]
$sql="select * from bn_profile where band_email=\"$username\" and password=\"$password\"";
[/code]

Doesn't look right to me...shouldn't it be....

[code]
$sql="select * from bn_profile where band_email=$username and password=$password";
[/code]

Share this post


Link to post
Share on other sites
No it shouldn't - you must surround string values with quotes, although most people probably use single ones.

I would suggest first trying the call to setcookie with only the first three parameters.

Or could it be that as you are doing a redirect straight away, there is no opportunity for the browser to send the cookie? Someone will be along in a minute who knows more than me about such things!

Share this post


Link to post
Share on other sites
Okay, sorry if I missled with my comment...I have never done it with double quotes and a backslash...as you say, single quotes is the norm, which I omitted to add

Share this post


Link to post
Share on other sites
Thanks guys,

Ive tried using just: setcookie("UN", $username, time()+3600);
but that didnt help (if thats what you meant)

The thing is ive used this exact script on a number of other hosting providers and its worked fine which is why im so confused and really need some help.... thanks

Share this post


Link to post
Share on other sites
Ok, have you set your browser's security settings to notify you when it recieves a cookie? Then at least you can tell if it's a problem writing the cookie or reading it back.

Share this post


Link to post
Share on other sites

×

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.