Jump to content

recording a page visit


ajetrumpet

Recommended Posts

hi guys,

I'm trying to use a very basic PHP script and i think I've got everything right but I'm not getting any records inserted into my table.  here is my script:

$dbHost = "ip address";
$dbName= "rptDatabase";
$dbUsername = "username";
$dbPassword = "password";
$ip = $_SERVER['REMOTE_ADDR'];
$page = $_SERVER['PHP_SELF'];
$referrer = $_SERVER['HTTP_REFERER'];
$date = date("m/d/y");
$time = date("h:i:a");

	$db = mysql_connect($dbHost, $dbUsername,
						$dbPassword) or trigger_error(mysql_error(),E_USER_ERROR);
 	mysql_select_db($dbName, $db);
	$sql = "INSERT INTO tblTraffic (ip, page, referrer, date, time) 
			VALUES ($ip, $page, $referrer, $date, $time)";
	$result = mysql_query($sql) or trigger_error(mysql_error(),E_USER_ERROR);
 	mysql_close($db);

can someone tell me what I'm doing wrong?  thanks.

Edited by ajetrumpet
Link to comment
Share on other sites

The list of what you are doing right might be shorter, however...

  1. You are using obsolete MySQL_ functions. Use PDO instead.
  2. string values in a query need to be inside quotes eg  VALUES ('$ip', '$page', ...etc)
  3. You should be using prepared queries so input values are not inserted directly into the query statement
  4. You should always store dates in a database in yyyy-mm-dd format 
  5. Instead of calculating current time and date in PHP then putting them in the query, just use a TIMESTAMP column which will update automatically
  6. You probably haven't got error reporting turned on
Edited by Barand
Link to comment
Share on other sites

1: I will look up PDO.  what functions am I using that are obsolete? 

6. I thought error reporting was always on in PHP.  How do I check for this?  I'm using the American Company Godaddy for my web hosting hosting and I manage mysql databases with phpMyAdmin.

Link to comment
Share on other sites

All function beginning with mysql_ are obsolete, replaced several years ago by either mysqli_ (improved) or PDO and removed completely from PHP 7.0 onwards.. The consensus is that PDO is better than mysqli.

I assumed it's turned off as you aren't apparently getting warning messages about the use of mysql_* functions. The settings are in your php.ini file. It could be the version of PHP is obsolete.

Link to comment
Share on other sites

well I downloaded eclipse and the version of php is 7.0 something, so the latest one I believe.  I can't find a php.ini file.  I remember seeing one last time I did this, but in eclipse when you begin a new project you only get a PROJECT file, and the setting is not in there.  where can i find the .ini file?

Link to comment
Share on other sites

Ok godaddy told me that error reporting is turned on by default and that if i wanted overwrite that file i need to create my own .user.ini file in my root dir.  And yes i have an error log file.  I will take a look in a little bit to see what it has logged.

Edited by ajetrumpet
Link to comment
Share on other sites

in my phpinfo() output, I see 4 related records:

Directive                      Local Value                  Master Value

display_errors             Off                                   Off

error_log                       error_log                        error_log

error_reporting          32767                             32767

log_errors                     On                                    On

 

here is my current code:

$dbHost = "ip address";
$dbName= "rptDatabase";
$dbUsername = "username";
$dbPassword = "password";
$ip = $_SERVER['REMOTE_ADDR'];
$page = $_SERVER['PHP_SELF'];
$referrer = $_SERVER['HTTP_REFERER'];
$date = date("m/d/y");
$time = date("h:i:a");

	$db = new mysqli($dbHost, $dbUsername, $dbPassword) or 
					trigger_error(mysql_error(),E_USER_ERROR);
 	mysql_select_db($dbName, $db);
	$sql = "INSERT INTO tblTraffic (ip, page, referrer, date, time) 
			VALUES ('$ip', '$page', '$referrer', '$date', '$time')";
	$result = mysql_query($sql) or trigger_error(mysql_error(),E_USER_ERROR);
 	mysqli_close($db);

going to the page throws numerous errors into the log:

 

[13-Oct-2019 18:20:06 UTC] PHP Notice: Undefined index: HTTP_REFERER in /home/name/public_html/DOMAIN/test/recordpageview.php on line 9

[13-Oct-2019 18:20:06 UTC] PHP Warning: mysqli::mysqli(): (28000/1045): Access denied for user 'username'@'ip-ipAddress.ip.secureserver.net' (using password: YES) in /home/home/public_html/DOMAIN/test/recordpageview.php on line 13

[13-Oct-2019 18:20:06 UTC] PHP Warning: mysql_select_db() expects parameter 2 to be resource, object given in /home/name/public_html/DOMAIN/test/recordpageview.php on line 15

[13-Oct-2019 18:20:06 UTC] PHP Warning: mysql_query(): Access denied for user 'username'@'localhost' (using password: NO) in /home/name/public_html/DOMAIN/test/recordpageview.php on line 18

[13-Oct-2019 18:20:06 UTC] PHP Warning: mysql_query(): A link to the server could not be established in /home/name/public_html/DOMMAIN/test/recordpageview.php on line 18

[13-Oct-2019 18:20:06 UTC] PHP Fatal error: Access denied for user 'username'@'localhost' (using password: NO) in /home/name/public_html/DOMAIN/test/recordpageview.php on line 18

 

this really doesn't make a whole lot of sense, does it?

Link to comment
Share on other sites

my new script after reading about preps is:

$dbHost = "ipaddress";
$dbName= "rptDatabase";
$dbUsername = "username";
$dbPassword = "password";
$ip = $_SERVER['REMOTE_ADDR'];
$page = $_SERVER['PHP_SELF'];
$referrer = $_SERVER['HTTP_REFERER'];
$date = date("m/d/y");
$time = date("h:i:a");

	$db = new mysqli($dbHost, $dbUsername, $dbPassword) or 
					trigger_error(mysql_error(),E_USER_ERROR);

	$stmt = $conn->prepare("INSERT INTO tblTraffic (ip, page, referrer, date, time) 
							VALUES (?, ?, ?, ?, ?)");
	$stmt->bind_param("sss", $ip, $page, $referrer, $date, $time);
	$stmt->execute();

	$stmt->close();
	$db->close();

and I get less errors, but still plenty:

[13-Oct-2019 18:59:36 UTC] PHP Notice: Undefined index: HTTP_REFERER in /home/name/public_html/DOMAIN/test/recordpageview.php on line 9

[13-Oct-2019 18:59:36 UTC] PHP Warning: mysqli::mysqli(): (28000/1045): Access denied for user 'username'@'ip-ipaddress.ip.secureserver.net' (using password: YES) in /home/ name/public_html/DOMAIN/test/recordpageview.php on line 13

[13-Oct-2019 18:59:36 UTC] PHP Notice: Undefined variable: conn in /home/name/public_html/DOMAIN/test/recordpageview.php on line 16

[13-Oct-2019 18:59:36 UTC] PHP Fatal error: Call to a member function prepare() on a non-object in /home/name/public_html/DOMAIN/test/recordpageview.php on line 16

Edited by ajetrumpet
Link to comment
Share on other sites

37 minutes ago, ajetrumpet said:

this really doesn't make a whole lot of sense, does it?

It does if you have some of your own.

38 minutes ago, ajetrumpet said:

13-Oct-2019 18:20:06 UTC] PHP Notice: Undefined index: HTTP_REFERER in /home/name/public_html/DOMAIN/test/recordpageview.php on line 9

There is no item in the $_SERVER array with the key "HTTP_REFERER"

 

39 minutes ago, ajetrumpet said:

[13-Oct-2019 18:20:06 UTC] PHP Warning: mysqli::mysqli(): (28000/1045): Access denied for user 'username'@'ip-ipAddress.ip.secureserver.net' (using password: YES) in /home/home/public_html/DOMAIN/test/recordpageview.php on line 13

The username and password you are using to connect are invalid

 

41 minutes ago, ajetrumpet said:

[13-Oct-2019 18:20:06 UTC] PHP Warning: mysql_select_db() expects parameter 2 to be resource, object given in /home/name/public_html/DOMAIN/test/recordpageview.php on line 15

You are calling a mysql_ function and pasing it a mysqli object. (You cannot mix'n'match)

Link to comment
Share on other sites

Barand,

 

This has taken quite a bit of my time and I'm getting a bit buggy in the brain.  If you have time, please review my last post, as I have written a new script with prepared query statements but i'm still getting the same errors.  I will try and correct some of the more obvious ones.

Edited by ajetrumpet
Link to comment
Share on other sites

This should fly better

<?php
$dbHost = "ipaddress";                          //   USE 
$dbName= "rptDatabase";                         //   VALID
$dbUsername = "username";                       //   CREDENTIALS
$dbPassword = "password";                       //   HERE

$ip = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : '';
$page = isset($_SERVER['PHP_SELF']) ? $_SERVER['PHP_SELF'] : '';
$referrer = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '';    // check they exist

$date = date("Y-m-d");
$time = date("h:i:a");

    mysqli_report(MYSQLI_REPORT_ERROR|MYSQLI_REPORT_STRICT);                 // tells mysqli to automatically report errors
    $conn = new mysqli($dbHost, $dbUsername, $dbPassword, $dbName);;

    $stmt = $conn->prepare("INSERT INTO tblTraffic (ip, page, referrer, date, time) 
                            VALUES (?, ?, ?, ?, ?)");
    $stmt->bind_param("sssss", $ip, $page, $referrer, $date, $time);
    $stmt->execute();

    $stmt->close();
    $db->close();
?>

 

Link to comment
Share on other sites

I added my username to the database as an authorized user and clicked "allow all privaleges".  As far as REFERER goes, everything I read on the web mentions nothing about an array section of the variable that I have to specify.  the code given is exactly what I have.  do you have a page you can point me to that shows me what you're talking about?  Here's a new error I'm getting as well:

 

[13-Oct-2019 19:36:59 UTC] PHP Warning: mysqli::prepare(): Couldn't fetch mysqli in /home/name/public_html/DOMAIN/test/recordpageview.php on line 17

Link to comment
Share on other sites

i tried:

$dbHost = "ip address";
$dbName= "rptDatabase";
$dbUsername = "username";
$dbPassword = "password";
$ip = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : '';
$page = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : '';
$referrer = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '';
$date = date("m/d/y");
$time = date("h:i:a");

	mysqli_report(MYSQLI_REPORT_ERROR|MYSQLI_REPORT_STRICT);
	$conn = new mysqli($dbHost, $dbUsername, $dbPassword, $dbName)

	$stmt = $conn->prepare("INSERT INTO tblTraffic (ip, page, referrer, date, time) 
						  	VALUES (?, ?, ?, ?, ?)");
	$stmt->bind_param("sssss", $ip, $page, $referrer, $date, $time);
	$stmt->execute();
	
	$stmt->close();
	$conn->close();

and I get only one error, but coming from you I can't understand why this would be thrown now:

[13-Oct-2019 20:39:37 UTC] PHP Parse error: syntax error, unexpected '$stmt' (T_VARIABLE) in /home/name/public_html/DOMAIN/test/recordpageview.php on line 16

Edited by ajetrumpet
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.   Restore formatting

  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.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.