Welcome to The Forum

Register now to gain access to all of our features. Once registered and logged in, you will be able to create topics, post replies to existing threads

PHP Help! Urgent!


---------
 Share

Recommended Posts

So what I am looking for is a simple php script, the first time the user visits the page it will say "You have been given 1 cookie". Then the second time the user visits the page it will say "You have been given 2 cookies". I'd like the number to not reset (back to 1) even if the person closes the program or shuts down their computer it will stay the same. If a database is needed I can provide one.

 

Thanks heaps in advanced.

 

 

 

What the page currently looks like, although the number does not change.

 

cookie.png

Link to comment
Share on other sites

http://stackoverflow.com/questions/9681580/displaying-a-message-as-pop-up-when-my-site-is-open

 

From an implementational standpoint, cookies might be the only way you can maintain this functionality

 

If a user clears it, they'll get the dialogue again saying they have one. PHP could pull a cookie's variable that counts the sessions

(e.g. $COUNT_IS = $SESSION_ID)

http://php.net/manual/en/function.session-id.php

 

More info... I'm not a programmer xD

Edited by enigma#
false I'm sure there are more than one ways to implement session continuity
Link to comment
Share on other sites

Thanks enigma, are you (or monkey or bort or whoever that can help) able to write the php that would do all of that, adding the number 1 to the total each time a user visits and keeping the data with cookies (meaning browser cookies, not yummy cookies, lol). At the moment I have nothing but HTML for the formatting and I have no clue where to start in coding the php. Sorry to be a pain, but I'm not very knowledgeable in php, I'm learning more though..! Thanks a million! :)

Edited by ENIGM0
Link to comment
Share on other sites

Enigma is Professional Googler! <3

 

Anyways, give me about 5 minutes to finish helping someone start a cs:s server and I will make a script for you...

 

tl;dr - Don't use cookies as they can be easily altered. Your right to use a database.

Link to comment
Share on other sites

I'm sorry I'm not a programmer,

I'm at most someone that likes to break codes =D

 

Monkey would be an excellent person to ask IMO =D

 

Ok, thanks enigma. :)

 

 

 

 

Enigma is Professional Googler! <3

 

Anyways, give me about 5 minutes to finish helping someone start a cs:s server and I will make a script for you...

 

tl;dr - Don't use cookies as they can be easily altered. Your right to use a database.

 

Thank you! :D I can also supply the database through my web host.

Edited by ENIGM0
Link to comment
Share on other sites

Enigma is Professional Googler! <3

 

Anyways, give me about 5 minutes to finish helping someone start a cs:s server and I will make a script for you...

 

tl;dr - Don't use cookies as they can be easily altered. Your right to use a database.

 

Like I said before, I'm not a programmer! :P

 

I just like to take code and break it so I can understand it =]

Link to comment
Share on other sites

eNiGMa you weren't wrong in suggesting using cookies. It's a viable solution to a simple task like this so you don't fill your database with a bunch of external IPs and cookie counts lol. However, like Papa John suggested using a database will be your best bet because you can utilize that information for other things such as, if user X has Y amount of cookies he is labeled cookie monster or something and you would be able to create those queries using your database. With a cookie you can't grab that information because it's stored on the client's computer. It really depends on the final product and how involved you want it because in reality you should have some sort of registration process or use an open ID API instead of relying on IP's or whatever method you use to identify your users browsing your site.

Link to comment
Share on other sites

eNiGMa you weren't wrong in suggesting using cookies. It's a viable solution to a simple task like this so you don't fill your database with a bunch of external IPs and cookie counts lol. However, like Papa John suggested using a database will be your best bet because you can utilize that information for other things such as, if user X has Y amount of cookies he is labeled cookie monster or something and you would be able to create those queries using your database. With a cookie you can't grab that information because it's stored on the client's computer. It really depends on the final product and how involved you want it because in reality you should have some sort of registration process or use an open ID API instead of relying on IP's or whatever method you use to identify your users browsing your site.

 

Yeah, I understand where your coming from, but I'd still rather use external IP address's in a database as it would be easier for the user than getting them to register and sign-in with open ID account. I also would rather a database instead of cookies so I can edit an IP's about of (edible) cookies, so I can give user X, 120 cookies by editing the database.

Link to comment
Share on other sites

Guest The_Monkey

I don't know the importance of using a database unless the count is super serious.

 

Here is how you do it with cookies:

 

@setcookie( "damn_cookie", $_COOKIE[ "damn_cookie" ] += 1, time() + 1036800 ); echo $_COOKIE[ "damn_cookie" ];

 

tada! Look at that sexyness.

 

Proof of it working:

http://monkeycodeworks.com/cookie-test.php

Edited by The_Monkey
Link to comment
Share on other sites

I don't know the importance of using a database unless the count is super serious.

 

Here is how you do it with cookies:

 

@setcookie( "damn_cookie", $_COOKIE[ "damn_cookie" ] += 1, time() + 1036800 ); echo $_COOKIE[ "damn_cookie" ];

 

tada! Look at that sexyness.

 

Proof of it working:

http://monkeycodeworks.com/cookie-test.php

 

Thanks heaps, thats what I'm using at the moment at http://enigmoweb.com/hg/command.jpg/. I really need to use a database as the page is being used in CS:S through an MOTD window. When I quit the game and open it back up, it resets both cookies (browser cookies and edible cookies).

 

 

 

http://pastebin.com/gg3sEH87

 

Sorry, I got distracted.

 

Edit: Slight fail (two results -.-) and added a way to get the clients cookies.

 

I used your php, put it in my database information and setup the database. I set the read/write access on the cookie-data.php file in my ftp to 777. I get the following error "Parse error: syntax error, unexpected ')', expecting ',' or ';' in /misc/33/000/241/168/8/user/web/enigmoweb.com/hg/command.jpg/cookie-data.php on line 34" on http://enigmoweb.com/hg/command.jpg/cookie-data.php.

 

He are some screenshots of the database's PHP MyAdmin Panel: (I manually entered in the IP address's shown through the INSERT tab)

db1.png

db2.png

db3.png

db4.png

db5.png

 

 

Thanks!

Edited by ENIGM0
Link to comment
Share on other sites

Oh, crap. I see what I have done with papa's php. I put the database info in the wrong quotation marks. I will fix it and see if it works.

 

 

EDIT: I have put the database information in the correct spot now, lol. Although it still doesn't work and gives me a database connect error.

 

Here is the database connect info now, obviously replacing * with sensitive info:

 

        define("DATABASE_USERNAME", "cookies"); // Database Username
       define("DATABASE_PASSWORD", "**"); // Database Password
define("DATABASE_HOST", "sql**.bravehost.com"); // Database Host
define("DATABASE_NAME", "cookies_*******"); // Database Name
define("REMOTE_IP", $_SERVER['REMOTE_ADDR']); // Client's IP

 

 

I fixed the error on line 32:

 

33.       if( mysql_query("UPDATE cookies SET cookienum = cookienum + 1 WHERE ip = '" . REMOTE_IP . "' LIMIT 1") ){
34.			echo "Cookies have been added"[color="#F00"])[/color];
35.		}else{
36.			die(mysql_error());
37.		}

 

Now it shows a database connect error, "Access denied for user 'cookies'@'172.16.%' to database 'DATABSE_NAME'"

Edited by ENIGM0
Link to comment
Share on other sites

echo "Cookies have been added");

to

echo "Cookies have been added";

 

Yeah, I have done that. Thanks! Now it shows a database connect error, "Access denied for user 'cookies'@'172.16.%' to database 'DATABSE_NAME'".

 

 

Current cookie-data.php code, again replacing * with sensitive information:

<?

/* Start Config */

define("DATABASE_USERNAME", "cookies"); // Database Username
define("DATABASE_PASSWORD", "**"); // Database Password
define("DATABASE_HOST", "sql**.bravehost.com"); // Database Host
define("DATABASE_NAME", "cookies_*******"); // Database Name
define("REMOTE_IP", $_SERVER['REMOTE_ADDR']); // Client's IP

/* Database Note:
	Table should be called: cookies
	Two text rows called: ip & cookienum
*/

/* End Config */

/* Connect to database */
mysql_connect(DATABASE_HOST, DATABASE_USERNAME, DATABASE_PASSWORD) or die(mysql_error());
mysql_select_db(DATABSE_NAME) or die(mysql_error());

/* Check Database For Record */
$query = mysql_query("SELECT * FROM cookies WHERE ip = '" . REMOTE_IP . "' LIMIT 1");
$result = mysql_query($query) or die(mysql_error());

/* If the record exists */
if( $result ){

	$row = mysql_fetch_array($result) or die(mysql_error());
	$CLIENT_TOTAL_COOKIES = $row['cookienum'] + 1;

	/* Since the record exists, lets update the amount of "cookies" they have */
	if( mysql_query("UPDATE cookies SET cookienum = cookienum + 1 WHERE ip = '" . REMOTE_IP . "' LIMIT 1") ){
		echo "Cookies have been added";
	}else{
		die(mysql_error());
	}

/* If it doesn't exist insert */
}else{

	/* Insert record into database */
	if( mysql_query("INSERT IGNORE INTO cookies ( ip, cookienum ) VALUES( '" . REMOTE_IP . "', '1'") ){
		echo "Your record has been added!";
	}else{
		die(mysql_error());
	}

}

?>

 

 

Monkey or Papa John, if you need the database host, username, password and name or access to the PHPMyAdmin panel; pm me.

Edited by ENIGM0
Link to comment
Share on other sites

Yeah, I have done that. Thanks! Now it shows a database connect error, "Access denied for user 'cookies'@'172.16.%' to database 'DATABSE_NAME'".

 

 

Current cookie-data.php code, again replacing * with sensitive information:

<?

/* Start Config */

define("DATABASE_USERNAME", "cookies"); // Database Username
define("DATABASE_PASSWORD", "**"); // Database Password
define("DATABASE_HOST", "sql**.bravehost.com"); // Database Host
define("DATABASE_NAME", "cookies_*******"); // Database Name
define("REMOTE_IP", $_SERVER['REMOTE_ADDR']); // Client's IP

/* Database Note:
	Table should be called: cookies
	Two text rows called: ip & cookienum
*/

/* End Config */

/* Connect to database */
mysql_connect(DATABASE_HOST, DATABASE_USERNAME, DATABASE_PASSWORD) or die(mysql_error());
mysql_select_db(DATABSE_NAME) or die(mysql_error());

/* Check Database For Record */
$query = mysql_query("SELECT * FROM cookies WHERE ip = '" . REMOTE_IP . "' LIMIT 1");
$result = mysql_query($query) or die(mysql_error());

/* If the record exists */
if( $result ){

	$row = mysql_fetch_array($result) or die(mysql_error());
	$CLIENT_TOTAL_COOKIES = $row['cookienum'] + 1;

	/* Since the record exists, lets update the amount of "cookies" they have */
	if( mysql_query("UPDATE cookies SET cookienum = cookienum + 1 WHERE ip = '" . REMOTE_IP . "' LIMIT 1") ){
		echo "Cookies have been added";
	}else{
		die(mysql_error());
	}

/* If it doesn't exist insert */
}else{

	/* Insert record into database */
	if( mysql_query("INSERT IGNORE INTO cookies ( ip, cookienum ) VALUES( '" . REMOTE_IP . "', '1'") ){
		echo "Your record has been added!";
	}else{
		die(mysql_error());
	}

}

?>

 

 

Monkey or Papa John, if you need the database host, username, password and name or access to the PHPMyAdmin panel; pm me.

 

Grant access to the database for the user 'cookies'.

Link to comment
Share on other sites

I just reread the code, nothing I typed is necessary..

 

Change:

mysql_select_db(DATABSE_NAME) or die(mysql_error());

to

mysql_select_db(DATABASE_NAME) or die(mysql_error());

 

^ Was missing an "A"

 

----------------------------------------------------------------------------------------

 

If you still need to know about privileges;

(what I typed beforehand)

Go to the query window (third icon under the phpmyadmin logo), and insert this query:

 

For all privileges:

GRANT ALL PRIVILEGES ON [database].* TO 'user'@'host'

 

For certain privileges:

GRANT [sELECT,INSERT,UPDATE,DELETE,CREATE,DROP] ON [database].* TO 'user'@'host'

 

Example:

GRANT SELECT,INSERT,UPDATE ON mydb.* TO 'dejaq'@'localhost'

And you can probably find some guides on Google.

 

P.S:

When you advance more in PHP, you should check out MySQLi.

Edited by Deja Q
Link to comment
Share on other sites

Sorry to be such a pain in the ass. I fixed DATABSE_NAME to DATABASE_NAME. It seems to connect now, but when I looked at the page, now I am getting another error, "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Resource id #3' at line 1".

 

EDIT:

 

I found 2 websites, which might help. Just not sure how to implement it into my php code.

http://www.phpbuilder.com/board/showthread.php?t=10350276

http://www.codingforums.com/showthread.php?t=142479

Edited by ENIGM0
Link to comment
Share on other sites

Sorry to be such a pain in the ass. I fixed DATABSE_NAME to DATABASE_NAME. It seems to connect now, but when I looked at the page, now I am getting another error, "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Resource id #3' at line 1".

 

PHP's too fun.

 

http://pastebin.com/La8255fh

 

Try that out, just changed a few things, can be viewed by "view the difference".

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share