Really simple question..

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
qleeq
Forum Newbie
Posts: 2
Joined: Mon Dec 07, 2009 2:54 am

Really simple question..

Post by qleeq »

Code: Select all

<?php 
$con = mysql_connect("~","~","~");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
elseif ($_SERVER['REMOTE_ADDR'] == "~")
{
echo "aa";
}
else {
mysql_select_db("~", $con);
mysql_query("INSERT INTO bulletins (ip, dateline) VALUES('Four-". $_SERVER['REMOTE_ADDR'] ."', ". time() .")") or die(mysql_error()); } ?>
                                                 
I've tried this and it works.
What this script is:
It's a script that upon loading, or viewing a page where it is embedded in the <img> tag, it records your IP address and unix timestamp into a database. There are different scripts to differentiate which page they are visiting..It's a shotty system but it's working for me right now.

Anyways, what I want to do is, if the IP address which loaded the page is mine, do nothing. Since there is no "do nothing" function i was just wondering if I could put

Code: Select all

echo "";
and have that give the same effect?

Also, is there any way to put the date + time in instead of the unix timestamp?
Like.. the month and date, and the time in either military or standard, pst-8 or gmt?
something like.. date("l F jS Y");
Also is there a way to tell which page they were viewing that had that script on it?
like for example, if someone viewed http://www.blah.net/home and it contained the script track1.php, it would add it to the database like this:
"65.182.4.4","123485892","www.blah.net/home"


PS, the ~ are just things I didn't want shown.
User avatar
Apollo
Forum Regular
Posts: 794
Joined: Wed Apr 30, 2008 2:34 am

Re: Really simple question..

Post by Apollo »

qleeq wrote:Anyways, what I want to do is, if the IP address which loaded the page is mine, do nothing. Since there is no "do nothing" function i was just wondering if I could put

Code: Select all

echo "";
and have that give the same effect?
Just do exit; or die; :)
Also is there a way to tell which page they were viewing that had that script on it?
like for example, if someone viewed http://www.blah.net/home and it contained the script track1.php, it would add it to the database like this:
"65.182.4.4","123485892","www.blah.net/home"
It doesn't work 100% (because people can configure their browser to hide this information) but the $_SERVER['HTTP_REFERER'] variable is probably what you need.
AlanG
Forum Contributor
Posts: 136
Joined: Wed Jun 10, 2009 1:03 am

Re: Really simple question..

Post by AlanG »

Right let's see...

1. Do nothing if you IP is found.

You could just rearrange your logic.

Code: Select all

if (!$con)
{
    die('Could not connect: ' . mysql_error());
}
elseif ($_SERVER['REMOTE_ADDR'] != "~") // If not your IP (no need for an else then)
{
    mysql_select_db("~", $con);
    mysql_query("INSERT INTO bulletins (ip, dateline) VALUES('Four-". $_SERVER['REMOTE_ADDR'] ."', ". time() .")") or die(mysql_error());
} ?>
2. Formatting a timestamp.

You can format a unix timestamp using the date function you mentioned. You can put a timestamp in as the 2nd parameter.

Code: Select all

$theTime = date("l F jS Y", $unixtime);
3. Page tracking.

You can use something like:

$location = 'http://' . $SERVER['SERVER_NAME'] . $_SERVER['PHP_SELF']; // Something like: http://www.blah.net/home

You will need to do this on the page they are viewing, and then pass that variable into the tracker. If you catch it at the tracker, you will only return the location of the tracker.

You don't really need to catch the domain name unless you are tracking multiple sites and placing the results in a communal database, but whatever floats your boat. :)
Last edited by AlanG on Mon Dec 07, 2009 3:21 am, edited 1 time in total.
qleeq
Forum Newbie
Posts: 2
Joined: Mon Dec 07, 2009 2:54 am

Re: Really simple question..

Post by qleeq »

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 'December 7th 2009)' at line 1

This is what I'm getting now..
AlanG
Forum Contributor
Posts: 136
Joined: Wed Jun 10, 2009 1:03 am

Re: Really simple question..

Post by AlanG »

qleeq wrote: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 'December 7th 2009)' at line 1

This is what I'm getting now..
A unix timestamp is a 10 digit integer. When you format it you will need to store it as a varchar. (You can store it as a datetime too, but you will need to format it as a datetime first).

So you will need to change the database schema (the table) to accomodate a varchar value. If you are still having problems, you will need to post your code aswell. Errors are useful, but only when they can be used to reference the actual code.
User avatar
Apollo
Forum Regular
Posts: 794
Joined: Wed Apr 30, 2008 2:34 am

Re: Really simple question..

Post by Apollo »

AlanG wrote:A unix timestamp is a 10 digit integer. When you format it you will need to store it as a varchar.
Eh, no, a unix timestamp is a regular unsigned 32-bit integer, so obviously you can simply store it as an UNSIGNED INT.
A BIGINT (which is 64-bit) would also work.

For timestamps before January 19, 2038, the 32th bit remains zero so then a regular INT will also do.
Post Reply