Page 1 of 2 12 LastLast
Results 1 to 10 of 12

Thread: web content depending on country?

  1. #1
    Join Date
    Jun 2006
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default web content depending on country?

    Hi I was wondering if anyone knew of a code or how to have different content depending on the country of the visitor to your site. For example I have seen on several sites, that once your IP is detected you're directed to content only available to the country from which you are visiting from. Does anyone have any idea how to do this? Is it possible with html?

    An answer would be appreciated!

    thanks

    -devin

  2. #2
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    1. NOT possible with just html.
    2. Javascript *might* work, but wouldn't be very compatible or trustworthy. It would also be quite hard to code, for a number of reasons.
    3. PHP (or another similar serverside programming language) is what you want.

    However... the IP doesn't say "USA"... it says an almost random series of numbers.

    I have no idea how they're distributed and I don't think it's an obvious setup, like everything from 1-3 for the first number is from USA, etc.
    However, there is some way of knowing it.
    The problem is that you need to know this. Handcoding it into the code would be a nightmare.
    Therefore, the only logical way to do it is to connect to a database that stores the info.
    Maybe google it and see if you can find out.
    If not, then you likely won't be able to do it.
    Notice that the companies doing this have a large budget to support it

    One possible suggestion would be to use php to take the IP, send it to a service that traces the IP and gives/suggests a country, then use that in the php code.
    However, note that some of the sites you can track IPs with say very clearly that it is ILLEGAL to perform operations on their site automatically... so be careful.

    If/once you have the country, you can use php to redirect to the right site, or set variables. Depends on what you want, exactly.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  3. #3
    Join Date
    Jun 2006
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Thanks for the reply djr33!

    It sounds more dificult than I imagined, and you're correct those web sites of those companies that do have those types of sites, do have very large budgets... perhaps I will just let this go and take the easy way out (just having each vititor select their country, of course I would have default content for any country not listed specifically. ).

    Thanks again for the reply!

    -devin

  4. #4
    Join Date
    May 2006
    Location
    Sydney, Australia - Near the coast.
    Posts
    1,995
    Thanks
    0
    Thanked 8 Times in 7 Posts

    Default

    Try: http://www.phplocation.com/

    But you will have to pay.


    Edit: You can get the free version here: http://www.phplocation.com/ip-countr...?product_id=18,
    Peter - alotofstuffhere[dot]com - Email Me - Donate via PayPal - Got spare hardware? Donate 'em to me :) Just send me a PM.
    Currently: enjoying the early holidays :)
    Read before posting: FAQ | What you CAN'T do with JavaScript | Form Rules | Thread Title Naming Guide

  5. #5
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Also, if a user does select their location, you could use cookies/sessions or a database relating their IP to location so that you don't need to have them "sign in" each time....

    However, the link that Tech just gave looks very promising. It might be a bit complex, but looks like it does exactly what you need, and it's from a company that has already dealt with the complexity of setting it up.... now you can just use it. Assuming the setup goes ok, I'd be very hopeful about that.
    Good luck
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  6. #6
    Join Date
    Jun 2006
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Thanks Tech, I will definetely check that out.

    By the way djr33, do you know if there is script for the cookies/sessions here @ dynamic drive? I am not to familiar with using cookies.

    Thanks

    -devin

  7. #7
    Join Date
    May 2006
    Location
    Sydney, Australia - Near the coast.
    Posts
    1,995
    Thanks
    0
    Thanked 8 Times in 7 Posts

    Default

    Peter - alotofstuffhere[dot]com - Email Me - Donate via PayPal - Got spare hardware? Donate 'em to me :) Just send me a PM.
    Currently: enjoying the early holidays :)
    Read before posting: FAQ | What you CAN'T do with JavaScript | Form Rules | Thread Title Naming Guide

  8. #8
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    I checked out the link and it was a big mess.
    I had to literally buy the software, for zero euros (no credit card, though).
    Then I had to unzip all that, look through the maze of files, to find that I need to "purchase" another free demo thing that allows me to decrypt the encrypted php scripts so I can view them.
    I had other stuff to do, so I bailed at that point.

    So... if you have the time to deal with that, looks quite promising... just annoying to get through all the commercial junk.

    As for cookies/sessions, there isn't a script here (though there may be some javascripts dealing with cookies, but that's not relevant), mostly because DD is more javascript than php... almost entirely.
    Sessions are really quite easy, and cookies aren't bad either (though they're not as good as sessions).

    Sessions go something like this:
    PHP Code:
    <?php
    session_start
    (); //this continues/begins the session
    //put session_start() at the top (or near the top, depending)
    //on every page you want to session to exist on.

    //now the $_SESSION[] array is available to you.

    $_SESSION['IP_address'] = $_SERVER['REMOTE_ADDR']; //store user's IP
    $_SESSION['name'] = $_POST['name']; //store 'name' from a form (method=post)
    $_SESSION['currentpage'] = "index.php"//store the page for the session
    $_SESSION['var'] = $var//etc.
    //these are just examples... you can do anything you want.

    ?>

    NEXT PAGE HERE:

    <?php
    //this is a new page...

    session_start(); //remember to continue the session...

    //with the session still going, we now have access to the old session vars.

    echo $_SESSION['IP_address']; //user's IP, stored on the last page.
    echo $_SESSION['var']; //should echo the value of $var from the last page.

    session_destroy(); //for security, or whatever, time to get rid of the session.

    ?>
    Notes:
    Sessions aren't anything special. Really, they are just a great tool that PHP has to use different methods of storing data easily accessible. The use cookies for the most part, but using cookies alone causes problems if the browser isn't compatible with them or the user has them disabled. Therefore, the session will notice this and solve it by using GET (index.php?var=value&...) and/or POST data from forms, etc. This ensures that it should be compatible for everyone. More reliable than cookies.
    One catch is that you must remember to continue (session_start()) the session each page, or there will be a hole in the chain and your data won't continue.

    Sounds confusing, maybe, but it's really not so bad, and they're very useful.


    Now, if you do get the above link/script to work, there's no need for this. If not, and you do have the user enter their location, then this would be helpful.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  9. #9
    Join Date
    May 2006
    Location
    Sydney, Australia - Near the coast.
    Posts
    1,995
    Thanks
    0
    Thanked 8 Times in 7 Posts

    Default

    Sorry, that was a random Google search. Here's a code if you want JavaScript cookies (Highly Not Recommended):

    Code:
    /*
    Script Name: Javascript Cookie Script
    Author: Public Domain, with some modifications
    Script Source URI: http://techpatterns.com/downloads/javascript_cookies.php
    Version 1.0.0
    Last Update: 30 May 2004
    
    This library 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.  
    */
    
    // this function gets the cookie, if it exists
    function Get_Cookie( name ) {
    	
    	var start = document.cookie.indexOf( name + "=" );
    	var len = start + name.length + 1;
    	if ( ( !start ) && ( name != document.cookie.substring( 0, name.length ) ) )
    	{
    		return null;
    	}
    	if ( start == -1 ) return null;
    	var end = document.cookie.indexOf( ";", len );
    	if ( end == -1 ) end = document.cookie.length;
    	return unescape( document.cookie.substring( len, end ) );
    }
    
    /*
    only the first 2 parameters are required, the cookie name, the cookie
    value. Cookie time is in milliseconds, so the below expires will make the 
    number you pass in the Set_Cookie function call the number of days the cookie
    lasts, if you want it to be hours or minutes, just get rid of 24 and 60.
    
    Generally you don't need to worry about domain, path or secure for most applications
    so unless you need that, leave those parameters blank in the function call.
    */
    function Set_Cookie( name, value, expires, path, domain, secure ) {
    	// set time, it's in milliseconds
    	var today = new Date();
    	today.setTime( today.getTime() );
    	// if the expires variable is set, make the correct expires time, the
    	// current script below will set it for x number of days, to make it
    	// for hours, delete * 24, for minutes, delete * 60 * 24
    	if ( expires )
    	{
    		expires = expires * 1000 * 60 * 60 * 24;
    	}
    	//alert( 'today ' + today.toGMTString() );// this is for testing purpose only
    	var expires_date = new Date( today.getTime() + (expires) );
    	//alert('expires ' + expires_date.toGMTString());// this is for testing purposes only
    
    	document.cookie = name + "=" +escape( value ) +
    		( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) + //expires.toGMTString()
    		( ( path ) ? ";path=" + path : "" ) + 
    		( ( domain ) ? ";domain=" + domain : "" ) +
    		( ( secure ) ? ";secure" : "" );
    }
    
    // this deletes the cookie when called
    function Delete_Cookie( name, path, domain ) {
    	if ( Get_Cookie( name ) ) document.cookie = name + "=" +
    			( ( path ) ? ";path=" + path : "") +
    			( ( domain ) ? ";domain=" + domain : "" ) +
    			";expires=Thu, 01-Jan-1970 00:00:01 GMT";
    }
    Peter - alotofstuffhere[dot]com - Email Me - Donate via PayPal - Got spare hardware? Donate 'em to me :) Just send me a PM.
    Currently: enjoying the early holidays :)
    Read before posting: FAQ | What you CAN'T do with JavaScript | Form Rules | Thread Title Naming Guide

  10. #10
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    No problem. Not complaining about you. The link looks very helpful, and probably is, once you can get around the mess of things to do with the setup. I hate now things are like that... so much work, for something. But it IS free, I suppose...
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •