1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Ajax Domain Check

Discussion in 'PHP' started by adamjblakey, Dec 20, 2007.

  1. #1
    Hi,

    I was wondering how i would get this script to work in an ajax style. At the moment i have this submitting to another page which checks but would be really good if i could type in the domain and select an ext and it automatically comes up if it available or not.

    Here is the code:

    <?php
    //Domain Checker
    function check_domain($domain,$ext){
    	$server="whois.networksolutions.com";
    	$nomatch="No match for";
    
    	$output="";
    	if(($sc = fsockopen($server,43))==false){
    		return true;
    	}
       	fputs($sc,"$domain.$ext\n");
       	while(!feof($sc)){
    		$output.=fgets($sc,128);
    	}
    	fclose($sc);
    	if (eregi($nomatch,$output)){
    		return true;
    	} else {
    		return false;
        }
    }
    
    //Sample usage
    $is_registered = check_domain("sonimager","com");
    if($is_registered == true){ echo "Domain Taken";}else{echo "Domain is free";}
    
    ?>
    Code (markup):
    Cheers,
    Adam
     
    adamjblakey, Dec 20, 2007 IP
  2. Barti1987

    Barti1987 Well-Known Member

    Messages:
    2,703
    Likes Received:
    115
    Best Answers:
    0
    Trophy Points:
    185
    #2
    Barti1987, Dec 20, 2007 IP
  3. coderbari

    coderbari Well-Known Member

    Messages:
    3,168
    Likes Received:
    193
    Best Answers:
    0
    Trophy Points:
    135
    #3
    domain.php
    
    <?php
    $domain = $_GET['domain'];
    $ext = $_GET['ext'];
    
    //Domain Checker
    function check_domain($domain,$ext){
    	$server="whois.networksolutions.com";
    	$nomatch="No match for";
    
    	$output="";
    	if(($sc = fsockopen($server,43))==false){
    		return true;
    	}
       	fputs($sc,"$domain.$ext\n");
       	while(!feof($sc)){
    		$output.=fgets($sc,128);
    	}
    	fclose($sc);
    	if (eregi($nomatch,$output)){
    		return true;
    	} else {
    		return false;
        }
    }
    
    //Sample usage
    $is_registered = check_domain($domain , $ext);
    if($is_registered == true){ echo "Domain Taken";}else{echo "Domain is free";}
    
    ?>
    
    PHP:
    ajax.php
    
    <html>
    <head>
    <title>Domain Age Calculator</title>
    <script language="javascript" type="text/javascript"> 
    //Browser Support Code
    function ajaxFunction(){
    	var ajaxRequest;  // The variable that makes Ajax possible!
    	
    	try{
    		// Opera 8.0+, Firefox, Safari
    		ajaxRequest = new XMLHttpRequest();
    	} catch (e){
    		// Internet Explorer Browsers
    		try{
    			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
    		} catch (e) {
    			try{
    				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
    			} catch (e){
    				// Something went wrong
    				alert("Your browser broke!");
    				return false;
    			}
    		}
    	}
    	// Create a function that will receive data sent from the server
    	ajaxRequest.onreadystatechange = function(){
    		if(ajaxRequest.readyState == 4){
    			var ajaxDisplay = document.getElementById('ajaxDiv');
    			ajaxDisplay.innerHTML = ajaxRequest.responseText;
    		}
    	}
    	var domain = document.getElementById('domain').value;
    	var ext = document.getElementById('ext').value;
    	
    	var queryString = "?domain=" + domain + "&ext=" + ext;
    	ajaxRequest.open("GET", "domain.php" + queryString, true);
    	ajaxRequest.send(null); 
    }
    
    </script>		
    </head>
    <body>
    <input type="text" size="30" name="domain" id="domain">
    <select name="ext" id="ext">
    	<option value="">select extension</option>
    	<option value="com">com</option>
    	<option value="org">org</option>
    	<option value="net">net</option>
    	<option value="info">info</option>
    	<option value="edu">edu</option>	
    </select>
    <input type="button" value="Check" onclick="ajaxFunction()"><br><br>
    <b>Enter the domain name without www. Correct</b> = gmail.com <b>Wrong</b> = www.gmail.com
    <br><br>
    <div id="ajaxDiv">The Message will be shown here</div>
    </body>
    </html>
    
    PHP:
    domain.php is the server side script.
     
    coderbari, Dec 20, 2007 IP
  4. adamjblakey

    adamjblakey Active Member

    Messages:
    1,121
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    80
    #4
    Thanks a lot coderbari's this works great. :)

    Just one thing though, the script comes back with domain name taken even if the domain is available. Any ideas on why this is happening?
     
    adamjblakey, Dec 21, 2007 IP
  5. coderbari

    coderbari Well-Known Member

    Messages:
    3,168
    Likes Received:
    193
    Best Answers:
    0
    Trophy Points:
    135
    #5
    I also got that error.This is may be bcoz of your written codes.I have no idea about that part.You wanted it to AJAXify and i gave you the idea.
     
    coderbari, Dec 21, 2007 IP
  6. Barti1987

    Barti1987 Well-Known Member

    Messages:
    2,703
    Likes Received:
    115
    Best Answers:
    0
    Trophy Points:
    185
    #6
    Debug

    After
    fclose($sc);

    Add
    echo $output;

    Read what it has.

    Peace,
     
    Barti1987, Dec 21, 2007 IP
  7. Agent_Dweeb

    Agent_Dweeb Peon

    Messages:
    5,607
    Likes Received:
    384
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Agent_Dweeb, Dec 21, 2007 IP
  8. tushardhoot1

    tushardhoot1 Active Member

    Messages:
    3,013
    Likes Received:
    96
    Best Answers:
    0
    Trophy Points:
    90
    #8
    tushardhoot1, Dec 21, 2007 IP