Need help with few lines of code

Discussion in 'PHP' started by balkanboy, Jun 6, 2008.

  1. #1
    Hi,

    I want to use Ip2Nation database to display some advertisements to USA visitors only.

    Here is the sample code with redirects, but I need 'echo' probably to display some html in page code.

    
    <?php
    	
    	$server   = ''; // MySQL hostname
    	$username = ''; // MySQL username
    	$password = ''; // MySQL password
    	$dbname   = ''; // MySQL db name
    	
    	
    	$db = mysql_connect($server, $username, $password) or die(mysql_error());
    	      mysql_select_db($dbname) or die(mysql_error());
    			
    	$sql = 'SELECT 
    	            country
    	        FROM 
    	            ip2nation
    	        WHERE 
    	            ip < INET_ATON("'.$_SERVER['REMOTE_ADDR'].'") 
    	        ORDER BY 
    	            ip DESC 
    	        LIMIT 0,1';
    	
    	list($country) = mysql_fetch_row(mysql_query($sql));
    	
    	switch ($country) {
    		case 'se':
    			// Get the swedish to a swedish newssite
    			header('Location: http://www.thelocal.se/');
    			exit;
    		case 'us':
    			// And let the folks from american go to CNN
    			header('Location: http://www.cnn.com/');
    			exit;
    		default:
    			// The rest can go to BBC
    			header('Location: http://www.bbc.co.uk/');
    			exit;
    	}
    	
    ?>			
    
    PHP:
    Can somebody give me code I need?

    Thanks in advance, Marko
     
    balkanboy, Jun 6, 2008 IP
  2. mikey1090

    mikey1090 Moderator Staff

    Messages:
    15,869
    Likes Received:
    1,055
    Best Answers:
    0
    Trophy Points:
    445
    Digital Goods:
    2
    #2
    
    <?php
       
        $server   = ''; // MySQL hostname
        $username = ''; // MySQL username
        $password = ''; // MySQL password
        $dbname   = ''; // MySQL db name
       
       
        $db = mysql_connect($server, $username, $password) or die(mysql_error());
              mysql_select_db($dbname) or die(mysql_error());
               
        $sql = 'SELECT
                    country
                FROM
                    ip2nation
                WHERE
                    ip < INET_ATON("'.$_SERVER['REMOTE_ADDR'].'")
                ORDER BY
                    ip DESC
                LIMIT 0,1';
       
        list($country) = mysql_fetch_row(mysql_query($sql));
       
        switch ($country) {
            case 'se':
                // Get the swedish to a swedish newssite
                header('Location: http://www.thelocal.se/');
                exit;
            case 'us':
           //display some banner adverts to the americans
          echo "<img src='you/already/know/how/to/write/html.jpg'>";
    
                exit;
            default:
                // The rest can go to BBC
                header('Location: http://www.bbc.co.uk/');
                exit;
        }
       
    ?>
    
    PHP:
     
    mikey1090, Jun 6, 2008 IP
  3. balkanboy

    balkanboy Banned

    Messages:
    1,950
    Likes Received:
    245
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Code is printing what I want to US visitors only, but it is not displaying any code below IP2Nation block.

    Here is code I used:

    
    <?php
       
        $server   = 'localhost'; // MySQL hostname
        $username = 'my username'; // MySQL username
        $password = 'my pass'; // MySQL password
        $dbname   = 'my database'; // MySQL db name
       
       
        $db = mysql_connect($server, $username, $password) or die(mysql_error());
              mysql_select_db($dbname) or die(mysql_error());
               
        $sql = 'SELECT
                    country
                FROM
                    ip2nation
                WHERE
                    ip < INET_ATON("'.$_SERVER['REMOTE_ADDR'].'")
                ORDER BY
                    ip DESC
                LIMIT 0,1';
       
        list($country) = mysql_fetch_row(mysql_query($sql));
       
        switch ($country) {
    
            case 'us':
           //display some banner adverts to the americans
          echo "<p>For Americans</p>";
                exit;
    			
            default:
                // The rest will not see anything
                echo "";
                exit;
        }
       
    ?>
    
    PHP:
     
    balkanboy, Jun 6, 2008 IP
  4. mikey1090

    mikey1090 Moderator Staff

    Messages:
    15,869
    Likes Received:
    1,055
    Best Answers:
    0
    Trophy Points:
    445
    Digital Goods:
    2
    #4
    Where? Where else should code be shown? What code?
     
    mikey1090, Jun 6, 2008 IP
  5. balkanboy

    balkanboy Banned

    Messages:
    1,950
    Likes Received:
    245
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Code that was generating the rest of the page bellow.

    Anyway, this is resolved now. Someone else helped me as you went off line on MSN Mikey.

    Thanks for your help
     
    balkanboy, Jun 6, 2008 IP
    mikey1090 likes this.