Jump to content

Recommended Posts

I'm trying to make my register script check the database's IP column and compare it with the user's IP. If the User's IP equals that in the DB column, it should say "Sorry, there is already an account registered with your IP Address. Please log in.", and if there's no IP match, it should allow them to continue with registering.

I've been tinkering around with this for a while and I can't seem to figure it out. Any help would be appreciated :)

 

 
if ($_SERVER['REMOTE_ADDR'] == mysql_query("SELECT ip FROM users")) {
	 die('Sorry, there is already an account registered with your IP Address. Please <a href="/login.php>log in.</a>');
		}else{
		echo '';
		}

 

I think the problem is with the mySQL query...

Link to comment
https://forums.phpfreaks.com/topic/218541-only-one-instance-of-an-ip-can-register/
Share on other sites

Then what happens when the user's IP address changes and it gets assigned to someone else that wants to sign up? What about a large company that uses a proxy server and all requests appear to be from the same IP address? Overall, trying to do it the way you describe is not a great idea.

I have a Text Captcha class I wrote! Very easy to use!

 

The class:

<?php
/*
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
//Created By James Little 
class TextCaptcha{
	private $question;
	private $xml;
	private $postVar = "realAns";
	private $salt = "Something Really Really Random, like 79";

	function __construct($api, $url = "http://textcaptcha.com/api/"){
		@session_start(); //Starts session if one has not already been started. @ messages any errors it may produce.
		if(!isset($_SESSION['~TC'])){ //So if called on page, does not send request more than once.
			//Grab question and answers!
			$url = $url.$api;
			try{
				$this->xml = @new SimpleXMLElement($url, NULL, true);
			}catch(Exception $e){
				//Could not Connect, this is the default
				$fallback =  '<captcha>'.
				'<question>How many wheels does a car have?</question>'.
				'<answer>'.md5(4).'</answer>
				<answer>'.md5("four").'</answer></captcha>';
				$this->xml = new SimpleXMLElement($fallback);
			}
			$this->setQA();
		}else
			$this->question = $_SESSION['~TC'];
	}
	function question(){
		return $this->question;
	}
	function refresh($title = "Refresh"){
		if(isset($_GET['newQuestion'])){
			unset($_SESSION['~TC']);
			header("Location: ".$this->getURL(false));
		}else
			return '<a href="'.$this->getURL(true).'newQuestion">'.$title.'</a>';
	}
	function correctAnswer($ans){
		$ra = $_POST[$this->postVar];
		unset($_SESSION['~TC']);
		$ans = $this->setAns($ans);
		for($i = 0; $i<count($ra); $i++){
			if($ra[$i] === $ans){
				return true;
			}
		}
		return false;
	}
	private function setQA(){
		//Set the questions and answers.
		$this->question = (string) $this->xml->question;
		$addOn = "";
		foreach ($this->xml->answer as $hash){
			$addOn .= '<input type="hidden" name="'.$this->postVar.'[]" value="'.$this->setAns((string) $hash, false).'" />';
		}
		$this->question = $addOn.$this->question;
		$_SESSION['~TC'] = $this->question;
	}
	private function getURL($fix){//This Function needs to be rewritten.
		$page = "http://".$_SERVER['HTTP_HOST'].$_SERVER['SCRIPT_NAME'];
		$page .= "?";
		if(count($_SERVER['QUERY_STRING']) > 0)
			$page .= $_SERVER['QUERY_STRING'];
		if($fix)
			$page .= "&";
		else
			$page = str_replace("&newQuestion", "", $page);
		return $page;
	}
	private function setAns($ans, $userInput = true){
		if($userInput) //Because the answers already come in as hashed, but they need to be reshash with salt
			return md5(md5(strtolower(trim($ans))).$this->salt);
		else
			return md5($ans.$this->salt);
	}
};
?>

 

An Example:

 

<?php
//To gain an API key, visit: http://textcaptcha.com/register
include("TextCaptcha.php");
$TextCaptcha = new TextCaptcha("MY API KEY HERE");

if(isset($_POST['submit'])){
	if($TextCaptcha->correctAnswer($_POST['answer']))
		echo "Correct, ";
	else
		echo "No, ";
	echo '<a href="?">Try Again!</a>';
	unset($_POST['submit']);
}else{

	echo '<form action="?" method="post" name="TC">';
	echo $TextCaptcha->question();
	echo " ";
	echo $TextCaptcha->refresh("New Question");
	echo '<br />';
	echo '<input type="input" name="answer" />';
	echo '<input type="submit" name="submit" value="Submit" />';
	echo '</form>';
}
?>

 

Also read that article posted, I know it will help you. Take the time to read. We do not post answers, but the path to the answers. Quit being lazy :/

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.