user3034002
user3034002

Reputation: 19

Array into MYSql

how to convert from an Array to string conversion I keep being prompt with error messages. Having tried the other recommendations but no luck. this is what i have so far.

<?php

// Create connection
$con=mysqli_connect("********","***","*****","blocklist");

// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }



    $query = "SELECT addresses from addresstbl;";  
$result = query($query,$error); 
if (!$result) { echo "error: $error"; }



$deny = array("$query");

if (in_array ($_SERVER['REMOTE_ADDR'], $deny)) {

header("location: http://www.google.com/");

exit();

} ?>

Upvotes: 1

Views: 54

Answers (3)

Cynthia Fridsma
Cynthia Fridsma

Reputation: 75

I think it would be a lot safer and faster if you work with a white list...

If the IP address is in your database, then continue, otherwise redirect the user to Google...

Something like this:

<?php
$user = "root";
$password = "";
$host = "localhost";
$dbname = "beta";
$site_id = "event_horizon";

$db = new mysqli($host, $user, $password, $dbname) or die("I can't make a connection with the database");

mysqli_select_db($db,$dbname);

$remote_ip = $_SERVER['REMOTE_ADDR'];


$query=("SELECT addresses from addresstbl WHERE addresses='".mysqli_real_escape_string($db,$remote_ip)."'");


$q = mysqli_query($db,$query);


if(mysqli_error($db)){
    echo "I'm sorry, but something went wrong";

    } 

$found = mysqli_num_rows($q);

if($found==0){
    header("location: http://www.google.com/");
} else {
    // you're IP address is not on the black list...
}
?>

Upvotes: 0

adamdude828
adamdude828

Reputation: 19

First of all , here is the link from php.net on how to use mysql_query. The result in this case will be a mysql resource and you have to use mysql_fetch_assoc to convert that result to an array that you can actually use. So you would want to use something like.

$mysqli = new mysqli('localhost', '***', '**', '***');

$result = $mysqli->query('select * from addresstbl');
if ($result) {
    $ip_result = array();
    while ($row = $result->fetch_assoc()) {
            $ip_result[] = $row['profile_name'];
    }

}
print_r($ip_result);

I hope that helps.

Upvotes: 1

sunny
sunny

Reputation: 1166

you can use .htaccess to block ip addresses, in your .htaccess file write-

order allow,deny
deny from ipaddress
deny from ipaddress 2
allow from all

Upvotes: 1

Related Questions