|
 |
|
Codewalkers Forums
> PHP Related
> PHP Coding
|
DB interactions - $var +1 adds 2 mysqli
Discuss $var +1 adds 2 mysqli in the PHP Coding forum on Codewalkers. $var +1 adds 2 mysqli Having problems with a PHP script you are coding? This is the place to get help!
|
|
|
|
 |
|
|
|
|

Codewalkers Forums Sponsor:
|
|
|

January 28th, 2013, 11:07 PM
|
|
Contributing User
|
|
Join Date: Apr 2007
Posts: 136
Time spent in forums: 2 Days 10 h 5 m 23 sec
Reputation Power: 7
|
|
|
DB interactions - $var +1 adds 2 mysqli
I have this code I'm trying write and I did have it using PDO but I had the same problem and I can't figure it out.
No matter which way I do it it adds 2 to the count instead of 1.
PHP Code:
$their_ip_address = $_SERVER['REMOTE_ADDR'];
$their_host = gethostbyaddr($their_ip_address);
$their_user_agent = $_SERVER['HTTP_USER_AGENT'];
if (array_key_exists("HTTP_REFERER", $_SERVER)) {
$their_referer = $_SERVER['HTTP_REFERER'];
} else {
$their_referer = "No referer set";
}
$mysqli_handler = new mysqli($config['db']['host'], $config['db']['username'], $config['db']['password'], $config['db']['dbname']);
if ($mysqli_handler->connect_errno) {
echo "Failed to connect to MySQL: " . $mysqli_handler->connect_error;
}
/* See if they are in the database */
$query = ("
SELECT *
FROM ip_blocker
WHERE ip = '$their_ip_address'
AND user_agent NOT LIKE '%google%'
AND user_agent NOT LIKE '%yahoo%'
AND user_agent NOT LIKE '%bing%'
AND user_agent NOT LIKE '%msn%'
AND user_agent not like '%slurp%'
");
//echo $their_ip_address.'<br>';
$result = $mysqli_handler->query($query) or die($mysqli_handler->error.__LINE__);
$rows = $result->fetch_assoc();
echo '<pre>'.print_r($rows, TRUE).'</pre>';
/* Check for any results. */
if (empty($rows)) {
/* They are not in the database, so add them. */
$query = ("
INSERT INTO ip_blocker
(
ip,
host,
count,
visits,
banned,
first,
user_agent,
referer
)
VALUES
(
$their_ip_address,
$their_host,
'1',
'1',
'0',
$time_now,
$their_user_agent,
$their_referer
)
");
$mysqli_handler->query($query) or die($mysqli_handler->error.__LINE__);
} else {
/* They are in the database, so start counting their page hits and add last time page was hit. */
$last = strtotime('now');
//echo $rows['count'].'<br>';
//$add_to_rows_count = $rows['count']+1;
//echo $add_to_rows_count.'<br>';
$query = ("
UPDATE ip_blocker
SET
count = count + 1,
last = $last
WHERE
id = ".$rows['id']."
");
$mysqli_handler->query($query) or die($mysqli_handler->error.__LINE__);
echo '<pre>'.print_r($rows, true).'</pre>';
exit;
I have tried it using:
$add_to_rows_count = $rows['count']+1;
And I get the same thing.
Any ideas why? I'm running out of hair...
__________________
Bob
|

January 29th, 2013, 05:53 AM
|
 |
Contributing User
|
|
Join Date: Apr 2007
Location: Galway
Posts: 1,369

Time spent in forums: 1 Month 4 h 23 sec
Reputation Power: 8
|
|
|
Is there a loop encompassing the block of code?
__________________
When I die, I want to go peacefully like my Grandfather did, in his sleep -- not screaming, like the passengers in his car.
|

January 29th, 2013, 05:59 AM
|
|
Contributing User
|
|
Join Date: Apr 2007
Posts: 136
Time spent in forums: 2 Days 10 h 5 m 23 sec
Reputation Power: 7
|
|
|
No...
And what normally comes after the "exit;" is code to ban them if the count is at the limit, then it will reset them back to 0 after the time limit.
It's written mostly for the the scraper sites that just pound the crap of of my servers I'm getting tired of it... And they keep changing ips so this is the best way to deal with them.
I'm thinking after 20 page hits within 300 secs it blocks them for 24 hours. Normal web surfers or slow search engines should be okay...
Last edited by rbrown : January 29th, 2013 at 06:06 AM.
|

January 29th, 2013, 03:53 PM
|
|
Contributing User
|
|
Join Date: Apr 2007
Posts: 136
Time spent in forums: 2 Days 10 h 5 m 23 sec
Reputation Power: 7
|
|
|
Figured it out...
The back story first...
I have an old version of Cold Fusion Studio 4.5 because I haven't found any other editors that have the editor screen output the way I like, fonts (I like arial 12pt, code highlighting etc.. CF Studio is awesome... However since it was written in 2000 as I have moved up in OS's some of the functions don't work so it is just an editor at this point.
Problem is Macromedia got their hands on CF Studio and f'n it up and over priced it and turned it into Dreamweaver (which would always mess with the code like almost as bad a MS Frontpage) then Adobe got it hands on it and did the same thing and really overpriced it...
So anyway... enough of the history lesson... ;) I went out looking for a new IDE and saw that Netbeans now has a php editor module. It is pretty close to CF Studio as far as the editor goes and I tweaked the code highlighting. The only downsides are no snippet library, the editor only shows one tab even through there are more than one, and the high lighting shows php vars colored inside of double quotes. It is a little slow once in a while when it is figuring out what you want to do next with the code completion.
So I installed the Debugger and stepped through the program and it worked as it was supposed to....
So scratching my head... I decided to switch browsers from Chrome and use Firefox and it works as it is supposed to. So for some reason Chrome is refreshing the page 2 times. And when I added the next section of code chrome refreshed 3 times added 3 to the count.
So I'm not sure what is going on with chrome but in the task manager when you start up chrome I get 3 exe's running and they (google) says this is for the plugins or monitoring your first born child or whatever...
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|