Forums

Create a Poll with ip block

GreyHead 16 Jan, 2014
Hi Smedia,

I think that this FAQ has the basic code you need except that (a) you'd need to add a database query to see if the address has already been used and (b) you'd need to show a polite message if it has.

Bob

PS In some parts of the world shared IP addresses are common, and in others they can change frequently (like mine does).
Smedia 16 Jan, 2014
Hi Bob,

Thnx for the quick reply!
That looks like it. Im not realy a php script wizzard so I have to check if it works :wink:
I think this code solves a part of my problem. But its not an option to manualy add all the ip adresses that are already used. We will be expecting many voters. Is there a way automate this proces via database check or some ohter way?

Smedia
GreyHead 17 Jan, 2014
Hi Smedia,

Yes, you'll need to add a DB query (or a DB Multi Record Loader action) to get a list of saved IP addresses to check against.

Bob
Smedia 27 Jan, 2014
Hi Bob,

Ive been trying and trying, but I cant get it to work. Everytime I test the php code it gives a
Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in... on line 11

Can you help me out?
Im using this php code in the custom code area:
<?php 
    mysql_connect("localhost","username","password") or die(mysql_error());  
    mysql_select_db("name database") or die(mysql_error());
    $ip = $_SERVER['REMOTE_ADDR'];

    # Full Text Attempt
    $query = mysql_query("SELECT `cf_ipadress` FROM `tablename` WHERE `cf_ipadress` = '$ip'");
    # Wild Card Attempt
    $query = mysql_query("SELECT `cf_ipadress` FROM `tablename` WHERE `cf_ipadress` LIKE '%$ip%'");
    # Use any one query from the above!
    if (mysql_num_rows($query) > 0)
	
    {
        header("Location: http://www.accesdenied.com");
        die();
    }
?>

Can you explain what is wrong with line 11?

Regards

Smedia
Smedia 27 Jan, 2014
Im aware the [ b ][ /b ] part of the code is wrong. Tried to highlite line 11.
Max_admin 27 Jan, 2014
Which Chronoforms version do you have ?
Max, ChronoForms developer
ChronoMyAdmin: Database administration within Joomla, no phpMyAdmin needed.
ChronoMails simplifies Joomla email: newsletters, logging, and custom templates.
Max_admin 28 Jan, 2014
Ok, let's change your code:

First a custom code action:

<?php 
    $form->data["user_ip"] = $_SERVER['REMOTE_ADDR'];
?>


Then a db record loader action with these settings:

Table: your table with data
Request param: user_ip
DB field: cf_ipaddress
Model: USERIP


Then another custom code action:

<?php
if(!empty($form->data["USERIP"])){
//redirect here, you may replace this code with a "custom server side validation" or "dynamic event switcher" with a redirect action
}


Regards,
Max
Max, ChronoForms developer
ChronoMyAdmin: Database administration within Joomla, no phpMyAdmin needed.
ChronoMails simplifies Joomla email: newsletters, logging, and custom templates.
Smedia 29 Jan, 2014
Hi Max,

Thnx for the help!
I copied all the scripts as you told me to do. But unfortunaltely with no working result.
The form still works the same as without the script.

Because I had a deadline on this form and I couldnt fix it, I decided to filter the data afterwards via the saved data.
It's not the way I wanted to do this, but in the long run the result will de the same.

I still want to solve this problem so I can make these forms work in future projects.
Do you have any other suggestions or changes in the code so it will work?

Regards,

Smedia
Max_admin 29 Jan, 2014
Hi Smedia,

You can add a "debugger" action after the "db record loader" and check the generated SQL statement, please post it here to make sure that it works as expected!

Regards,
Max
Max, ChronoForms developer
ChronoMyAdmin: Database administration within Joomla, no phpMyAdmin needed.
ChronoMails simplifies Joomla email: newsletters, logging, and custom templates.
GreyHead 30 Jan, 2014
Hi Smedia,

I think that there is a small typo in Max;s code, he used 'user_id' in one block and 'USERID' later on.

I'd do the same thing all in Custom Code:
<?php 
$ip = $_SERVER['REMOTE_ADDR'];
if ( !$ip ) {
  return false; // how are you going to handle this case?
}
$db =& JFactory::getDBO();
$ query = "
SELECT COUNT(`cf_ipadress`) 
  FROM `tablename` 
  WHERE `cf_ipadress` = '{$ip}' ;
";
$db->setQuery($query);
$count = $db->loadResult();
if ( $count > 0 ) {
  $app =& JFactory::getApplication();
  $app->redirect( 'http://www.accesdenied.com');
}
?>

Bob
Smedia 04 Feb, 2014
He Bob and Max,

Sorry for the slow reply.
I will try the code, but I dont know exactly when. First I have some other priorities to coop with.
I will post the feedback when I tested it.

Regards,

Smedia
Smedia 13 Feb, 2014
Hello,

Well I finally got the ip block script to work! 🙂
I didnt use your codes, but I composed one myself from different php sql examples on the net.
Here the result I endedup with:

<?php 
    $sql = sprintf("SELECT ipaddress FROM table_name WHERE ipaddress = '%s'",
             mysql_real_escape_string($_SERVER['REMOTE_ADDR'])
        );
    $query = mysql_query($sql);
    if(mysql_num_rows($query) > 0) {
      $row = mysql_fetch_array($query);
      $mainframe->redirect('http://www.yoururl.com');
    }
?>


Thnx for the help!

Regards,

Smedia
GreyHead 13 Feb, 2014
Hi Smedia,

If it works OK that's fine; the code looks as though it is pretty much the same but without using the Joomla! DB methods.

Bob
This topic is locked and no more replies can be posted.