Need Help with Session Varibales

Discussion in 'PHP' started by inhook, Dec 7, 2008.

  1. #1
    Guys, I need to show Country Specific ads to the users of my website. This works well intially when i am able to get the users Ip address and get the Country Code and set a session variable to that country and use that variable to show ads for that country.

    But I also have flags (links) on my website for each country the user could go to and browse that countrys items. How do i reset the session variable at that time to the exact flag that the user has clicked.

    Here is what i have until now

      <?php
    // index.php
    
    session_start();
    require_once('ip.php');
    //this variable below is set in ip. php
    $_SESSION['country'] = based on the users country;
    
    //then somewhere in the index.php i have images with href
    <a href="index.php" target="_self"><img src="images/UK.png" />UK</a>
    
    // how do i reset the $_SESSION['country'] = based on what the user has selected;
    
    
    PHP:

     
    inhook, Dec 7, 2008 IP
  2. mac83

    mac83 Active Member

    Messages:
    237
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    58
    #2
    You could set two session variables
    user_country
    selected_country

    So when a user clicks on the different flag, set the session variable for selected country.

    Now when you display ads, make a if else statement

    if (isset($_SESSION['selected_country']) && ($_SESSION['selected_country']!='') ) {
    // Show this country ads
    } else {
    // User Country ads
    }
     
    mac83, Dec 7, 2008 IP
  3. EricBruggema

    EricBruggema Well-Known Member

    Messages:
    1,740
    Likes Received:
    28
    Best Answers:
    13
    Trophy Points:
    175
    #3
    See the post above (removed mine, above has more info)
     
    EricBruggema, Dec 7, 2008 IP
  4. inhook

    inhook Well-Known Member

    Messages:
    248
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    108
    #4
    thank you mac83 thank makes sense the only problem now i have is how to set the session variable on the click of the hyperlink, if a sample code is provided that will be good.
     
    inhook, Dec 7, 2008 IP
  5. mac83

    mac83 Active Member

    Messages:
    237
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    58
    #5
    Your Link:
    
    echo '<a href="'.$_SERVER['PHP_SELF'].'?selected_country=UK" target="_self"><img src="images/UK.png" />UK</a>'
    
    PHP:
    On the top of the page:
    
    if ( isset($_GET['selected_country'])&&($_GET[$_GET['selected_country']]!='') ) {
     $_SESSION['selected_country'] = $_GET['selected_country'];
    }
    
    
    
    PHP:
     
    mac83, Dec 7, 2008 IP
    inhook likes this.
  6. inhook

    inhook Well-Known Member

    Messages:
    248
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    108
    #6
    thank you very much.
    Rep added.
     
    inhook, Dec 7, 2008 IP