How to restrict access to a web site from a particular geographical area - web-hosting

I want to restrict all the users from a particular geographical area from accessing my web site.

You can use PHP to do this:
<?php
$ip = $_SERVER['REMOTE_ADDR'];
$file = file_get_contents("http://api.hostip.info/?ip={$ip}");
if(stristr($file, 'bad country') === FALSE) {
echo 'ya\'ll may visit this here site.';
}
else {
echo "nope!";
}
?>

Related

Google recaptcha when posting to different server

I have a form with recaptcha V2:
https://www.fisherwallace.com/pages/do-you-qualify-to-use-the-device
It posts to a different server and then redirects back to a different page on the server with the recaptcha form.
Recaptcha site says: "We detected that your site is not verifying reCAPTCHA solutions." I assume it's due to posting to the different server.
NOTE: You'll see I have a clumsy workaround at the moment to address the fact that the recaptcha does not challenge automatically. Without the workaround, the recaptcha is there but nothing happens on submit.
I found some sample PHP code for the server side...
$email;$comment;$captcha;
if(isset($_POST['email'])){
$email=$_POST['email'];
}
if(isset($_POST['comment'])){
$comment=$_POST['comment'];
}
if(isset($_POST['g-recaptcha-response'])){
$captcha=$_POST['g-recaptcha-response'];
}
if(!$captcha){
echo '<h2>Please check the the captcha form.</h2>';
exit;
}
$secretKey = "Put your secret key here";
$ip = $_SERVER['REMOTE_ADDR'];
// post request to server
$url = 'https://www.google.com/recaptcha/api/siteverify?secret=' . urlencode($secretKey) . '&response=' . urlencode($captcha);
$response = file_get_contents($url);
$responseKeys = json_decode($response,true);
// should return JSON with success as true
if($responseKeys["success"]) {
echo '<h2>Thanks for posting comment</h2>';
} else {
echo '<h2>You are spammer ! Get the #$%K out</h2>';
}
Clearly lots of this code is moot since the user does not actually load the POST server page.
But will this part get the callback recaptcha needs?
$secretKey = "Put your secret key here";
$ip = $_SERVER['REMOTE_ADDR'];
// post request to server
$url = 'https://www.google.com/recaptcha/api/siteverify?secret=' . urlencode($secretKey) . '&response=' . urlencode($captcha);
$response = file_get_contents($url);
$responseKeys = json_decode($response,true);
// should return JSON with success as true
If the IP is a mismatch, can i hard code it using the IP from the originating server?
IP is optional
You need to do file_get_contents 'method' => 'POST' for siteverify

PHP - Global Variables

I am trying to dynamically set database connection credentials based on who logs into a web page. I'm pretty sure it's not working because of the $connectdb variable not being defined. Can someone please check out my code and try to get it working? Thanks!
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
$connectdb="";
class Main extends CI_Controller {
function __construct() {
parent::__construct();
echo $connectdb;
$this->load->database($connectdb);
$this->load->helper('url');
$this->load->library('grocery_CRUD');
}
public function index() {
if ($_POST["username"] == "root") {
global $connectdb="default";
}
if ($_POST["username"] == "user1") {
global $connectdb="user1";
}
if ($_POST["username"] == "user2") {
global $connectdb="user2";
}
$connect = #mysql_connect("localhost", $_POST["username"], $_POST["password"]);//won't display the warning if any.
if (!$connect) {
echo 'Server error. Please try again sometime. CON';
} else {
print("Employees");
echo "<br>";
print("Visitors");
}//Just an example to ensure that we get into the function
// LOAD LIBRARIES
}
public function employees() {
$this->grocery_crud->set_table('employees');
$output = $this->grocery_crud->render();
$this->_example_output($output);
}
public function visitors() {
$this->grocery_crud->set_table('visitors');
$output = $this->grocery_crud->render();
$this->_example_output($output);
}
function _example_output($output = null) {
$this->load->view('our_template.php',$output);
}
}
A quick read of THE MANUAL will show you it's pretty easy to have multiple database connections. You define the connection parameters in your database.php config file then call the database with the group name.
if($user == 'someguise'){
$this->load->database('guiseDB');
}
HTH
For something as important as this i would strongly suggest running the form input through CI form validation first. You really should be validating and doing things like limit the number of characters, make sure its letters only, trim whitespace and XSS cleaning - all before you do anything else. (this helps your user as well)
then to get the value from the form - do something like this
$username = $this->input->post( 'username', TRUE ) ;
and work with the one variable $username. the TRUE XSS cleans the value, and then instead of repeating
$_POST["username"] ==
over and over and over, you are just checking $username. also makes the code easier to read. if you need $username in different methods just use:
$this->username = $this->input->post( 'username', TRUE ) ;
and then $this->username will work in any method in the class.
finally consider having a table of users or config list -- and then use a different value to call your database. in other words maybe they log in with the user name: "root" but then its a different name like global $connectdb = "rootadmin"

How can we redirect to previous page after login in cibonfire?

How can we redirect to previous page after login in cibonfire?
If some module require login to view, than the code there will check for login and if it is not logged in, it will redirect to login page. After login we want to get back to same module. How can we do so.
Thanks
Before your checking ,A user is already loged in ,Add
$this->session->set_userdata('page_url', current_url());
Add in your login function ,After checking the login data.
if($this->session->userdata('page_url'))
redirect($this->session->userdata('page_url'));
else
redirect('default home page');
At the end of your controller function add this line
redirect('your_view_here', $any_variables_here);
I use sessions for logging in and for navegation tasks like that, because it just makes life a lot simpler. In my controller functions I drop in a line of code like the following:
$this->load_segs(func_get_args(), strtolower(__CLASS__).'/'.__FUNCTION__);
Here's the load segements function:
function load_segs($params, $src){
if ( ! is_array($params) || ! isset($src)){
return FALSE;
}
foreach ($params as $key => $value){
//only include values that don't need encoding
if ($value != urlencode($value)){
break;
}
$src .= '/'.htmlentities($value, ENT_QUOTES, 'UTF-8');
}
$this->session->set_userdata('return', $src);
return $src;
}
You can retrieve the information as follows:
$return = html_entity_decode($this->session->userdata('return'));
redirect($return);

How to verify the ownership of a website?

I want to create a tag to allow user to claim the ownership of his website.
How would I get the tag and the match it against the one I have generated.
The tag format will be <!--tag-->
how will I verify this tag if I am able to get the html using file_Get_contents("url");
it's just a string match.
<?php
$tagCode = "<!-- abc123 -->";
$pageContent = file_get_contents("http://www.somesite.com/");
if (strpos($pageContent, $tagCode) !== false) {
echo "it matched!";
}

Codeigniter - best routes configuration for CMS?

I would like to create a custom CMS within Codeigniter, and I need a mechanism to route general pages to a default controller - for instance:
mydomain.com/about
mydomain.com/services/maintenance
These would be routed through my pagehandler controller. The default routing behaviour in Codeigniter is of course to route to a matching controller and method, so with the above examples it would require an About controller and a Services controller. This is obviously not a practical or even sensible approach.
I've seen the following solution to place in routes.php:
$route['^(?!admin|products).*'] = "pagehandler/$0";
But this poses it's own problems I believe. For example, it simply looks for "products" in the request uri and if found routes to the Products controller - but what if we have services/products as a CMS page? Does this not then get routed to the products controller?
Is there a perfect approach to this? I don't wish to have a routing where all CMS content is prefixed with the controller name, but I also need to be able to generically override the routing for other controllers.
If you use CodeIgniter 2.0 (which has been stable enough to use for months) then you can use:
$route['404_override'] = 'pages';
This will send anything that isn't a controller, method or valid route to your pages controller. Then you can use whatever PHP you like to either show the page or show a much nicer 404 page.
Read me guide explaining how you upgrade to CodeIgniter 2.0. Also, you might be interested in using an existing CMS such as PyroCMS which is now nearing the final v1.0 and has a massive following.
You are in luck. I am developing a CMS myself and it took me ages to find a viable solution to this. Let me explain myself to make sure that we are on the same page here, but I am fairly certain that we area.
Your URLS can be formatted the following ways:
http://www.mydomain.com/about - a top level page with no category
http://www.mydomain.com/services/maintenance - a page with a parent category
http://www.mydomain.com/services/maintenace/server-maintenance - a page with a category and sub category.
In my pages controller I am using the _remap function that basically captures all requests to your controllers and lets you do what you want with them.
Here is my code, commented for your convenience:
<?php
class Pages extends Controller {
// Captures all calls to this controller
public function _remap()
{
// Get out URL segments
$segments = $this->uri->uri_string();
$segments = explode("/", $segments);
// Remove blank segments from array
foreach($segments as $key => $value) {
if($value == "" || $value == "NULL") {
unset($segments[$key]);
}
}
// Store our newly filtered array segments
$segments = array_values($segments);
// Works out what segments we have
switch (count($segments))
{
// We have a category/subcategory/page-name
case 3:
list($cat, $subcat, $page_name) = $segments;
break;
// We have a category/page-name
case 2:
list($cat, $page_name) = $segments;
$subcat = NULL;
break;
// We just have a page name, no categories. So /page-name
default:
list($page_name) = $segments;
$cat = $subcat = NULL;
break;
}
if ($cat == '' && $subcat == '') {
$page = $this->mpages->fetch_page('', '', $page_name);
} else if ($cat != '' && $subcat == '') {
$page = $this->mpages->fetch_page($cat, '', $page_name);
} else if ($category != "" && $sub_category != "") {
$page = $this->mpages->fetch_page($cat, $subcat, $page_name);
}
// $page contains your page data, do with it what you wish.
}
?>
You of course would need to modify your page fetching model function accept 3 parameters and then pass in info depending on what page type you are viewing.
In your application/config/routes.php file simply put what specific URL's you would like to route and at the very bottom put this:
/* Admin routes, login routes etc here first */
$route['(:any)'] = "pages"; // Redirect all requests except for ones defined above to the pages controller.
Let me know if you need any more clarification or downloadable example code.

Resources