php include question

Discussion in 'Integration' started by jdvernier, Feb 8, 2011.

  1. jdvernier

    jdvernier New Member

    Joined:
    Nov 22, 2010
    Messages:
    9
    Hi all,

    I have a situation a bit different than the scenario offered in the aMember user manual.

    I have a page in my website that I want to have links appear dynamically if a user is registered and has purchased product #3, 4 or 5. If a user is registered and is associated with any other product, the links should not appear.

    I don't want the full functionality as decribed for PHP Include in the user's manual - in other words, I don't want the user redirected to the login page if they attempt to click on a "protected" link. I would rather that the link not display at all if a user has not purchased the proper level of access. Instead, users who don't have the correct permissions will simply see some text describing the genre of the song (this is for an online radio station).

    Here's what I am doing:

    In the header:
    Code:
    $_product_id = array(3,4,5); 
    
    In the space where the link will appear for users with the correct access:
    Code:
    if (array_intersect((array)$_SESSION['_amember_product_ids'],(array)$_product_id)) {
      echo '<a href="' . $song["request"] . '"><img src="/images/request.gif" alt="Request this song now!" border="0" /></a>';
    } else {
      echo '<span style="font-size:.9em;">' . $song['genre'] . '</span>';
    }
    
    Note that until I added the aMember-specific code checking for the product_id, this page worked as intended.

    In theory, when the user is assigned to product 3, 4 or 5, they will see a button that links to a page on our site where they can make a song request. (The song request displays in a popup window, which is why I am trying to circumvent aMember's stock PHP Include functionality). If the user doesn't have those permissions, they simply see some text describing the genre for that song.

    Of course, this is not working as I intended. A test account I am using has the proper product ID, but when I navigate to the page in question - I should see the button, but I am instead seeing the "else" conditional.

    To take a look at the page I am talking about: http://www.radio-opie.com/library.php.

    You can log in as the following user: username - test; password - test. This account is associated with product #4, so in theory if you use this username and password you should see the buttons - but you don't.

    Can anybody see where I am making my mistake? I think it's in how the session is handling the product_id field.

    Thanks!
  2. alexander

    alexander Administrator Staff Member

    Joined:
    Jan 8, 2003
    Messages:
    6,279
    try to output $_SESSION[_amember_product_ids] and $_product_id before you use them in if statement, and make sure that they both have correct values.
    PHP:
    print_r($_SESSION[_amember_product_ids]);
    print_r($_product_id);
  3. jdvernier

    jdvernier New Member

    Joined:
    Nov 22, 2010
    Messages:
    9
    Thanks Alexander. I am going to give this a try in a bit when I am home from work and let you know how it turned out.
  4. jdvernier

    jdvernier New Member

    Joined:
    Nov 22, 2010
    Messages:
    9
    OK this is what I got:

    From the dump of $_SESSION[_amember_product_ids]:
    Array ( [0] => 5 [1] => 1 )

    From the dump of $_product_id:
    Array ( [0] => 3 [1] => 4 [2] => 5 )

    Anticipating your next question, I did a print_r for the actual array_intersect call and got this result:
    Array ( [0] => 5 )

    So now I'm really confused. In theory, since I have a match in the array_intersect call, the "IF" conditional should kick in and my buttons should appear.
  5. jdvernier

    jdvernier New Member

    Joined:
    Nov 22, 2010
    Messages:
    9
    Got it!

    I noticed that one part of my code was working - the heading for the column in question should show the word "Genre" if not logged in, but should be blank if logged in with right permissions. This was working.

    This page displays search results from our song library. The results page uses a table for the results. A function is used to loop through the results and write a row to the table for each match.

    Once I saw the heading was working correctly (which was outside the function) but the individual rows were not working (which was inside the function), I figured we were now dealing with an issue of variable scope. A quick line of code declaring a global $_product_id within my row-writing function made the request buttons work as designed.

    Thanks for pointing me in the right direction, Alexander. That's two times you've saved my rear on this project! I'll have to buy you a beer sometime.

    Thanks again,

    John

Share This Page