Page 1 of 1

Cookies Not working!

PostPosted: 16. August 2006 16:37
by godsdead
Okie, My cookies just arnt working on my Xampp, they used to, no matter what script i use i cant get my cookies to work..

so i thought id do the most simple cookie check..
Code: Select all
<?
setcookie ("tomsservercookie", "Cookie worked!");
echo $_COOKIE["skin"];
?>


i refreshed the page about 6 times and then it saved it..
but it wouldnt let me update it..
This is gettign really annying as i cant figur out what the hell it is.
my firewall is letting through cookies.
Iv tryed different browser.
Iv cleared my cookies and cache..

Im stuck!
Please helppp!

PostPosted: 16. August 2006 16:45
by Wiedmann
so i thought id do the most simple cookie check..

One simple question:
You have read the PHP manual about cookies, and/or the description from setcookie() and the examples you can see there?

PostPosted: 16. August 2006 21:46
by godsdead
no.. but this should work? to set a basic cookie?
And none of the largly known scripts about the web, cookies work either..
:(
I really need this sorted :(

PostPosted: 16. August 2006 22:29
by Wiedmann
no.. but this should work? to set a basic cookie?

Well, the manual shows me, that your 2 line script can't work.

You should better come back with a working test script, to talk about (you can find a similar one as yours in the manual).

PostPosted: 17. August 2006 08:17
by godsdead
AHA! okie, well i read that bit on the php manual, and got these examples and edited them a bit and found out that when i run my xampp server through a different domain (im useing No-ip, godsdead.sytes.net)
My cookies are stored! yay!
But they dont work for http://localhost
=[ which is stupid as i would prefer to work offline :D

heres the code i used;

Code: Select all
<?php

// Get domain without www and any port (if present)
$domain = getDomain();

// Set expiration (30 days)
$expire = time() + (86400*30);

// Set the cookie
setcookie('cookie[one]', 'adminyes', $expire, '/', $domain);

echo"You Created a cookie, <a href=cookie.php?show=yes>VIEW THEM!</a>";

function getDomain() {
   if ( isset($_SERVER['HTTP_HOST']) ) {
       // Get domain
       $dom = $_SERVER['HTTP_HOST'];
       // Strip www from the domain
       if (strtolower(substr($dom, 0, 4)) == 'www.') { $dom = substr($dom, 4); }
       // Check if a port is used, and if it is, strip that info
       $uses_port = strpos($dom, ':');
       if ($uses_port) { $dom = substr($dom, 0, $uses_port); }
       // Add period to Domain (to work with or without www and on subdomains)
       $dom = '.' . $dom;
   } else {
       $dom = false;
   }
   return $dom;
}

if($show == "yes"){
echo"
<b>Your Cookies for this server are as followed;</b><br>
";

if (isset($_COOKIE['cookie'])) {
   foreach ($_COOKIE['cookie'] as $name => $value) {
       echo "$name : $value <br />\n";
   }
}
}else{}

?>

PostPosted: 17. August 2006 10:29
by WorldDrknss
here a sample cookie script
Code: Select all
<?php
setcookie("muser", $musername, time()+86400, "/", "", "0");
$muser = $_COOKIE['muser'];
echo ($muser);
?>


another example:
Code: Select all
<?php
include('../database.php');
$musername = $_POST['musername'];
$mpassword = $_POST['mpassword'];
$self = $_SERVER['PHP_SELF'];
$referer = $_SERVER['HTTP_REFERER'];

if((!$musername) or (!$mpassword))
{
header("Location:$referer"); exit();
}
$sql = "select * from users where username='$musername' and password=password('$mpassword')";
$rs = @mysql_query($sql, $conn) or die("Could not execute query");
$num = mysql_num_rows($rs);

if($num !=0){
setcookie("muser", $musername, time()+86400, "/", "", "0");
setcookie("msite", "domain.com", time()+86400, "/", "", "0");
setcookie("mauth", "ok", time()+86400, "/", "", "0");
header("Location:/index.php"); exit();
}
else
{
header("Location:$referer");
}
?>


here is the cookie layout:
Code: Select all
setcookie("Name", "Value", "Expiry", "Path", "Domain", "Security");

PostPosted: 17. August 2006 13:21
by Wiedmann
godsdead wrote:My cookies are stored! yay!
But they dont work for http://localhost

Right. In your last (correct) sample script you are using the domain parameter in setcookie(). This does not work for "localhost" at the most times.

BTW:
In line 31 you should use $_GET['show'] and not $show.