473,404 Members | 2,195 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

Cookie will never work on a PHP page no matter what I do!

tpgames
785 512MB
I've tried JS cookie on the html page that contains the iFrame code linking to the php jigsaw puzzle game. That did NOT work.
I've tried JS cookie on the php page; php cookie on the php page, php cookie on the html page, and none of those have worked.

[PHP]<?php
session_start();
$rating1=$rating;
session_destroy();
$today=time();
$atoday=getdate($today); //Get the date into an array
$atoday->mon++; //set the month to next month
$month=$atoday->mon;
$year=$atoday->year;
if ($month > 12)
{
$month=1;
$year++;
}
$cexpire=mktime(0,0,0,$month,1,$year);
$linkid=$QUERY_STRING; //get the link id number as an integer.
global $slinkid, $cname;
$slinkid=strval($linkid); //convert to a string
$cname="test";
setcookie($cname,$slinkid,$cexpire);
?> [/PHP]
Expand|Select|Wrap|Line Numbers
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  2. <html>
  3. <head>
  4. <title> Jigsaw Puzzles Online </title>
  5. <meta content="text/html; charset=iso-8859-1" http-equiv="Content-Type">
  6. <link type="text/css" rel="stylesheet" href="http://www.tpgames.net/a/a.css"> 
  7.  
  8. <script type="text/javascript">
  9. <
  10. function getCookie( name ) {
  11.   var start = document.cookie.indexOf( name + "=" );
  12.   var len = start + name.length + 1;
  13.   if ( ( !start ) && ( name != document.cookie.substring( 0, name.length ) ) ) {
  14.     return null;
  15.   }
  16.   if ( start == 2 ) return null;
  17.   var end = document.cookie.indexOf( ";", len );
  18.   if ( end == 2 ) end = document.cookie.length;
  19.   return unescape( document.cookie.substring( len, end ) );
  20. }
  21.  
  22. function setCookie( name, value, expires, path, domain, secure ) {
  23.   var today = new Date();
  24.   today.setTime( today.getTime() );
  25.   if ( expires ) {
  26.     expires = expires * 1000 * 60 * 60 * 24;
  27.   }
  28.   var expires_date = new Date( today.getTime() + (expires) );
  29.   document.cookie = name+"="+escape( value ) +
  30.     ( ( expires ) ? ";expires="+expires_date.toGMTString() : "" ) + //expires.toGMTString()
  31.     ( ( path ) ? ";path=" + path : "" ) +
  32.     ( ( domain ) ? ";domain=" + domain : "" ) +
  33.     ( ( secure ) ? ";secure" : "" );
  34. }
  35.  
  36. function deleteCookie( name, path, domain ) {
  37.   if ( getCookie( name ) ) document.cookie = name + "=" +
  38.     ( ( path ) ? ";path=" + path : "") +
  39.     ( ( domain ) ? ";domain=" + domain : "" ) +
  40.     ";expires=Thu, 01-Jan-1970 00:00:01 GMT";
  41. }
  42.  
  43. </script>
  44.  
Link to jigsaw game
Mar 5 '07 #1
12 2312
tpgames
785 512MB
Here is the PHP game code. Is there something in the code that make it impossible for a cookie to work?

[PHP]<?php for($i = 1; $i <= 5; $i++) echo "<a href=\"jponline.php?level=$i\">Level $i</a><br/>\n"; ?>
</td></tr>





</table>

<?php

$rows = $cols = max(2, min(10, 2 * (isset($_GET['level']) ? $_GET['level'] : 2)));

for($r = 0; $r < $rows; $r++)
{
for($c = 0; $c < $cols; $c++)
{
$h[$r][$c] = (rand(0, 100) > 50) ? 1 : 0;
$v[$r][$c] = (rand(0, 100) > 50) ? 1 : 0;
}
}

$files = glob('images/*.jpg');
$img = $files[rand(0, count($files) - 1)];
list($width, $height) = getimagesize($img);

$d = floor(min($width / $cols, $height / $rows) / 5);

$vw = floor($width / $cols);
$vh = floor($height / $rows);

$iw = $vw + 2 * $d;
$ih = $vh + 2 * $d;

$xinc = $vw;
if($xinc % 4 == 0) $xinc /= 4;
elseif($xinc % 3 == 0) $xinc /= 3;
elseif($xinc % 2 == 0) $xinc /= 2;

$yinc = $vh;
if($yinc % 4 == 0) $yinc /= 4;
elseif($yinc % 3 == 0) $yinc /= 3;
elseif($yinc % 2 == 0) $yinc /= 2;

$img = urlencode(basename($img));
unset($files);

for($r = 0; $r < $rows; $r++)
{
for($c = 0; $c < $cols; $c++)
{
$g = chr(65+bindec($v[$r][$c] . (1-$v[$r+1][$c]) . $h[$r][$c] . (1-$h[$r][$c+1])));
$src = "chop.php?img=$img&r=$r&c=$c&cc=$cols&rr=$rows&d=$ d&g=$g";
echo "<img style=\"position:absolute\" name=\"t_{$r}_{$c}\" src=\"$src\" alt=\"\" width=\"$iw\" height=\"$ih\"/>\n";
$list[] = "t_{$r}_{$c}";
}
}
$list = '"' . join('","', $list) . '"';
?>

<script type="text/javascript">
SET_DHTML(<?=$list?>);

var cluster = new Array();

for(r = 0; r < <?=$rows?>; r++)
{
for(c = 0; c < <?=$cols?>; c++)
{
id = 't_' + r + '_' + c;
obj = dd.elements[id];
obj.moveTo(<?=$xinc?> * Math.round(Math.random() * 800 / <?=$xinc?>), <?=$yinc?> * Math.round(Math.random() * 600 / <?=$yinc?>));
obj.row = r;
obj.col = c;
obj.cluster = id;
cluster[id] = new Array();
cluster[id][id] = id;
}
}

function merge_cluster(a, b)
{
for(i in cluster[b])
{
obj = dd.elements[cluster[b][i]];
cluster[a][obj.name] = obj.name;
obj.cluster = a;
}
}

function align_cluster(a)
{
for(i in cluster[a])
{
obj = dd.elements[cluster[a][i]];
obj.moveTo(<?=$xinc?> * Math.round(obj.x / <?=$xinc?>), <?=$yinc?> * Math.round(obj.y / <?=$yinc?>));
}
}

function my_DragFunc()
{
for(i in cluster[dd.obj.cluster])
{
obj = dd.elements[cluster[dd.obj.cluster][i]];
if(obj.name != dd.obj.name)
{
ox = dd.obj.x + (obj.col - dd.obj.col) * <?=$vw?>;
oy = dd.obj.y + (obj.row - dd.obj.row) * <?=$vh?>;
obj.moveTo(ox, oy);
}
}
}

function my_DropFunc()
{
align_cluster(dd.obj.cluster);

for(r = -1; r <= 1; r++)
{
for(c = -1; c <= 1; c++)
{
if(c != 0 || r != 0)
{
obj = dd.elements['t_'+(dd.obj.row + r)+'_'+(dd.obj.col + c)];
if(obj)
{
if(dd.obj.x + c * <?=$vw?> == obj.x && dd.obj.y + r * <?=$vh?> == obj.y)
{
merge_cluster(dd.obj.cluster, obj.cluster);
}
}
}
}
}

}

</script>[/PHP]
Mar 5 '07 #2
tpgames
785 512MB
Anyone? :>)
Mar 8 '07 #3
tpgames
785 512MB
To clarify, I am looking for a PHP cookie that will work in this jigsaw puzzle game. AND a how-to put it in, WHERE to put it, and anything else needed to get the cookie to actually work. The game does not come up how I left it when I go back to the page. Do I need a 'enter name here' form in order for the cookie to work? Thanks! (Sorry, I'm a complete newbie and PHP is NOT my language at all.)
Mar 8 '07 #4
tpgames
785 512MB
I need it to actually SAVE the progress of the game so that the user can go back to the SAME game with the pieces in the same places. I am about to go back to trying JS again, as the PHP programmers don't seem to know. I'm clueless. I've read up on cookies, and don't understand them.
Thanks!
Mar 11 '07 #5
tpgames
785 512MB
I am bumping this!

bump
Mar 13 '07 #6
tpgames
785 512MB
Shoot me! I've double posted this, sort of, in the JavaScript forum, because I thought maybe I might get a JS cookie to work, being that NO ONE knows why the PHP cookie does not work, NOR do they know why the PHP code does not LET cookies work, NOR do they know what in the world I am doing wrong ! I'm assuming the experts are all on a 2 week vacation. :>)

I think that this is a valid reason for double posting! I can't wait until I become an expert in languages that are ignored so that I can answer some of these posts! Too bad it will take me 3 years. Cognitive disorder = takes longer. But, once I get it, I'll have it. And, I was told that 24 hours was long enough to wait. I think that 9 days is long enough to wait. Patience IS a virute after all. However, I give up!
Mar 13 '07 #7
ronverdonk
4,258 Expert 4TB
I don't know very much about cookies and games because I never use them. I prefer good old Mysql to that. There are obviously no other members who can help you out on this.

But I found a PHP class that can handle cookies in a zillion different ways. So, for your problem, you could have a look at that class (from phpclasses.org) and see if it fits your requirement. It can be found at Cookie Objects

What it can do:
This class was designed to store and retrieve objects made of composite data items of a custom type in cookies. The information is stored like a bidimensional array of n objects of m items.

This classe can be used in applications based on shopping carts.

The class provides means to:

- Add new objects
- Add new items to each object
- Store all objects information in a cookie
- Test if a cookie was previously stored
- Retrieve the objects information from a cookie
- Delete an object from the list of objects
- Remove cookies
Ronald :cool:
Mar 13 '07 #8
Atli
5,058 Expert 4TB
Hi.

This line will create a cookie that expires in 30 days.
[PHP]
// Expiration time = time() * secs * mins * hours * days
setcookie("cName", "cValue", time() + 60*60*24*30);
[/PHP]

// This line will read the cookie value
[PHP]
$cValue = $_COOKIE['cName'];
[/PHP]

If you need more info goto www.php.net
Mar 14 '07 #9
tpgames
785 512MB
Thanks! Because I am dealing with a jigsaw puzzle that seems to ignore regular cookie code, I think rondervacs (sp?) might actually work! All the other regular codes didn't do a thing.

I'll post later to day when I find out if it worked or not.
Mar 14 '07 #10
tpgames
785 512MB
Trying to save a Game IN Progress:
I downloaded the class files - they are for a shopping cart.

I am unsure how to get everything to work.
What I need to do is save the jigsaw puzzle game exactly how the user left it so they can come back and finish the game later.
The problem is that when I go back to the page, it gives me a brand new game.

So what do I need to change in the class files to make it collect the correct data to save the jigsaw puzzle game?
What do I call the data?
Thanks!

The txt version of GAME php file with OLD cookie I tried using:

PHP link 1


These are the Cookie class PHP files:
Cookie Objects php file

Send Data php file

View Data php file

Get Data php file
Mar 14 '07 #11
tpgames
785 512MB
Sorry Ronald! I slaughtered your username!
:>)
Mar 14 '07 #12
tpgames
785 512MB
acoder gave me some good insight in the JS forum on cookies. The result is that I'm going to have to figure out what part of the code make people move and figure out how to capture that movement in a cookie. I have no idea, but am going to do some extensive reading to see if I can find out how. I'm still trying to understand PHP and JS.
thanks!
Mar 15 '07 #13

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: dan glenn | last post by:
Say, I want to set a cookie and have it expire an hour after it's set. It's looking like this is only possible for browsers which are in the same time zone as my server?? In other words, if I...
0
by: Michael | last post by:
I have a problem here that only seems to happen on the one new server that we are migrating our ASP.Net application to. The server is a Windows 2000 SP4 box. It initially had the .Net Framework...
15
by: Oleg Leikin | last post by:
Hi, (newbie question) I've created some simple .NET ASP application that should store cookies at the client machine. According to the documentation cookie expiration time is set via...
2
by: | last post by:
Today I learned that creating cookies inside of a custom class in ASP.NET 2.0 requires that you prefix it with HttpContext.Current..., e.g. : ...
17
by: Bruno | last post by:
I have a feature that is hosted on a different domain from the primary one in a frame, and need to retain values in a cookie. example: A web page at one.com contains a frame which has a page...
2
by: HopfZ | last post by:
Server sends cookie to browser and the browser send the same cookie back to the server according to Wikipedia. Do browsers send even javascript-generated cookie to servers? For example, if I...
29
by: Jerim79 | last post by:
I did try to find the answer to this before posting, so this isn't a knee jerk reaction. What I am trying to accomplish is to have a script that opens a cookie, reads a value, and then use a...
5
by: mosscliffe | last post by:
I have the following code, which I thought would create a cookie, if one did not exist and on the html form being sent to the server, it would be availabe for interrogation, when the script is run...
3
by: rjoseph | last post by:
Hi Guys I hope this is an easy one for you. Basically I have a page on my website that creates a javascript based cookie on the users pc. The contents of the cookie when created looks as...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.