I'm Learning But Stumped... Session Help

A beginner who has learned much the past few weeks but needs a bit of

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

32 Replies - 1557 Views - Last Post: 17 December 2008 - 02:39 AM Rate Topic: -----

#1 RayRayAngel  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 1
  • View blog
  • Posts: 39
  • Joined: 15-December 08

I'm Learning But Stumped... Session Help

Posted 15 December 2008 - 02:01 PM

Hello Everyone!

I've been working on a game using the Facebook Platform and have much work completed in the past week. Now I've got several parts of this game completed that I've been working on and learned a great deal about PHP and MySQL working together. I've got a game that uses PHP to modify an SQL database extensively but I have one feature that is key to the success of the game that I am stumped on. To be honest I'm not even sure where to begin but I have some general ideas.

I basically want to set a session I believe when a person first visits the game for the day. Facebook passes along the user information each time we have a hit on the page. I want to take that information (which is stored in a database using the following code)
$con = mysql_connect("$db_ip","$db_user","$db_pass");
if (!$con)
  {
  die('Error Adding UserID: ' . mysql_error());
  }
mysql_select_db("$db_name", $con);
mysql_query("INSERT INTO Users (UserID) 
VALUES ('$user')");
// Create Record From User ID Number


and throw in a logged in time to the database such as (start_time) and then start a countdown from four hours. Basically each user gets a token bonus every four hours that they login to spend inside the game. So basically if they come back to the application within four hours they won't get the bonus but after the fourth hour from the previous login they will. I already have created the code that awards the bonus and it is here:
mysql_query("UPDATE Users SET Units = '$new_currency' WHERE UserID='$fetch_id'");

The $new_currency function is elsewhere in the code and performs the fetch of the original currency plus the new currency before updating the SQL query.

I'm thinking the best way to do this is a PHP session but honestly I have no idea. My original thought was that I need to store in the database the login time and then use some form of PHP to countdown those four hours before executing the "bonus" section of the code.

Any thoughts? What would be the best way to go about this? I'm new to this so if you need more information or I need to do a better job explaining what I need please let me know.

Is This A Good Question/Topic? 0
  • +

Replies To: I'm Learning But Stumped... Session Help

#2 alexander7567  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 21
  • Joined: 26-November 08

Re: I'm Learning But Stumped... Session Help

Posted 15 December 2008 - 02:44 PM

you might could set a cookie with the time, but thats kinda a bad idea in case they dont have cookies enabled. thats just an idea after quickly skimming over your post
Was This Post Helpful? 0
  • +
  • -

#3 RayRayAngel  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 1
  • View blog
  • Posts: 39
  • Joined: 15-December 08

Re: I'm Learning But Stumped... Session Help

Posted 15 December 2008 - 02:46 PM

I was also thinking that but it appears that Facebook restricts the use of cookies so I need to find a way to do this with just PHP sadly.
Was This Post Helpful? 0
  • +
  • -

#4 alexander7567  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 21
  • Joined: 26-November 08

Re: I'm Learning But Stumped... Session Help

Posted 15 December 2008 - 02:53 PM

oh i see... hmm... i guess i would use a database such as MySQL then.. thats the only other thing i know to store info. But make sure you also include something that will clean up ur database!
Im kinda new also, but i have advanced in mysql, so ccokies and mysql are rly the only thing i kno to use
Was This Post Helpful? 0
  • +
  • -

#5 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2834
  • View blog
  • Posts: 9,740
  • Joined: 08-August 08

Re: I'm Learning But Stumped... Session Help

Posted 15 December 2008 - 06:19 PM

It sounds like you want to keep track even if the user shuts down their system and logs in from another one after four hours. If that's the case, the only way to do it would be to store the information on the server end, either through a MySQL database or a text file. The database would probably be easier, especially if you expect to have many users.

By the way, sessions keep track of data until the user quits their browser, or your code destroys the session. There may be a time limit, but if there is I'm not aware of it.
Was This Post Helpful? 1
  • +
  • -

#6 RayRayAngel  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 1
  • View blog
  • Posts: 39
  • Joined: 15-December 08

Re: I'm Learning But Stumped... Session Help

Posted 15 December 2008 - 09:06 PM

I do have a database that stores lots of game information such as prizes won, credits the players have and what not. So I would need to find a way to store time logged in at I guess.... then find a way in PHP to see if four hours have passed since that last time. Does that make sense? If so I need to figure out how to go about that.
Was This Post Helpful? 0
  • +
  • -

#7 Valek  Icon User is offline

  • The Real Skynet
  • member icon

Reputation: 541
  • View blog
  • Posts: 1,708
  • Joined: 08-November 08

Re: I'm Learning But Stumped... Session Help

Posted 15 December 2008 - 09:12 PM

Run a query at login to put the then-current value of time() into the database. Then, at next login, have it take the current value of time() and subtract the database-stored value from it. If it's equal to or greater than 14400, four hours have passed. Question is, do you want it to go four hours cumulatively, or 4 hours since last login (as in, having the four hour counter reset if they login prior to four hours, or keep going until 4 hours pass regardless of the number of login attempts)?

This post has been edited by Valek: 15 December 2008 - 10:18 PM

Was This Post Helpful? 1
  • +
  • -

#8 RayRayAngel  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 1
  • View blog
  • Posts: 39
  • Joined: 15-December 08

Re: I'm Learning But Stumped... Session Help

Posted 15 December 2008 - 09:38 PM

Valek,

I think that will do exactly what I want and I feel shameful for never thinking about that. I want to go four hours without reseting during that time. So I need to find a way to make that execute only one time until the next time they visit.
Was This Post Helpful? 0
  • +
  • -

#9 Valek  Icon User is offline

  • The Real Skynet
  • member icon

Reputation: 541
  • View blog
  • Posts: 1,708
  • Joined: 08-November 08

Re: I'm Learning But Stumped... Session Help

Posted 15 December 2008 - 09:43 PM

Okay, so make your "last logged in" check for the four-hours credit addition only update when it adds a credit :)
Was This Post Helpful? 0
  • +
  • -

#10 RayRayAngel  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 1
  • View blog
  • Posts: 39
  • Joined: 15-December 08

Re: I'm Learning But Stumped... Session Help

Posted 15 December 2008 - 09:52 PM

I'm going to try this later tonight and let you know the results. I appreciate the help everyone has provided!! :) :) :)
Was This Post Helpful? 0
  • +
  • -

#11 Valek  Icon User is offline

  • The Real Skynet
  • member icon

Reputation: 541
  • View blog
  • Posts: 1,708
  • Joined: 08-November 08

Re: I'm Learning But Stumped... Session Help

Posted 15 December 2008 - 09:57 PM

You're very welcome. I look forward to hearing how this works out for you :)

This post has been edited by Valek: 15 December 2008 - 09:58 PM

Was This Post Helpful? 0
  • +
  • -

#12 alexander7567  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 21
  • Joined: 26-November 08

Re: I'm Learning But Stumped... Session Help

Posted 15 December 2008 - 10:16 PM

View PostCTphpnwb, on 15 Dec, 2008 - 05:19 PM, said:

There may be a time limit, but if there is I'm not aware of it.


just so u know for future reference, there is no time limit. Just until they close their browser like u said.

and from what i read, that code he gave u should work just fine! good luck!
Was This Post Helpful? 0
  • +
  • -

#13 RayRayAngel  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 1
  • View blog
  • Posts: 39
  • Joined: 15-December 08

Re: I'm Learning But Stumped... Session Help

Posted 16 December 2008 - 12:30 AM

Maybe its my ignorance but the number I get from the following doesn't seem to be right.
$con = mysql_connect("$db_ip","$db_user","$db_pass");
if (!$con)
  {
  die('Error: ' . mysql_error());
  }

mysql_select_db("$db_name", $con);

$result = mysql_query("SELECT * FROM Users
WHERE UserID='$fetch_id'");

while($row = mysql_fetch_array($result))
  {
  $original_login = $row['Login'];
  }
// Fetch Original Login Time

$current_time = time();
// Fetch Current Time

$new_time = $current_time - $original_time;
// Do The Math

echo $new_time;


Right now I'm just testing that the time value is correct. I set the original value for time elsewhere using:
mysql_query("UPDATE Users SET Login = '$current_time' WHERE UserID='$user'");
// Update Database String


When I take those two values and do the math with a calculator I get a number I expect like 804. But when I do that math in PHP I don't get the same value. I'm pretty sure its something in my syntax. I'm confident I'll figure it out but I wouldn't have even been this far without the time value suggestion ;)
Was This Post Helpful? 0
  • +
  • -

#14 Valek  Icon User is offline

  • The Real Skynet
  • member icon

Reputation: 541
  • View blog
  • Posts: 1,708
  • Joined: 08-November 08

Re: I'm Learning But Stumped... Session Help

Posted 16 December 2008 - 03:36 AM

Your variable names are different. You're setting the time to $original_login, but you're subtracting current time from $original_time, which is never set. Try it while making those variable names match.
Was This Post Helpful? 0
  • +
  • -

#15 alexander7567  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 21
  • Joined: 26-November 08

Re: I'm Learning But Stumped... Session Help

Posted 16 December 2008 - 03:37 AM

try echoing all the variables and see what they are without the script. That's is my number one troubleshooter!
Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3