Help please with basic code for sign in/sign out menu link

Discussion in 'aMember Pro v.4' started by ak48avk, Mar 11, 2013.

  1. ak48avk

    ak48avk Angela Kane

    Joined:
    Aug 9, 2010
    Messages:
    124
    I'm all for keeping my site simple and up until now I have pretty well kept visitors and members in different areas of my site.

    I am making changes as I can see the benefit of common areas and keeping protected areas mainly for downloads and videos rather than pages. More pages and links are then available for google crawling and other search engines. Also, visitors get the flavour of what members get without having access to the downloads.

    I have had different menus depending on the areas but now I am using a standard menu so I would like my login/ logout / link to reflect whether a member is signed in or not. I know it's not difficult but I don't know much about php and any help would be much appreciated.

    Thanks.
  2. thehpmc

    thehpmc Member

    Joined:
    Aug 24, 2006
    Messages:
    901
    Have a look at http://www.amember.com/docs/API/Lite

    All you have to do to have appropriate 'login/ logout / link' is to have a conditional statement based on response of 'Am_Lite::getInstance()->isLoggedIn() '

    For instance if Am_Lite::getInstance()->isLoggedIn() display logout link else display login link.
  3. ak48avk

    ak48avk Angela Kane

    Joined:
    Aug 9, 2010
    Messages:
    124
    Thanks very much. I thought it was something to do with API/Lite.

    Any chance of you giving me some example code to place in my html? I've checked the docs file but need a little guidance to do the conditional statement.

    I'm a machine code/assembler/cobol programmer from way, way back but don't dabble in much code these days!

    A little more guidance and I can see how useful this would be for all conditional links on my pages, for example links to my members downloads and videos rather than having separate member pages.

    Assistance much appreciated.
  4. alexander

    alexander Administrator Staff Member

    Joined:
    Jan 8, 2003
    Messages:
    6,279
    First Am_Lite API code will work inside php scripts only.
    Place this line at the top of your script which display menu:
    PHP:
    <?php include "/full/path/to/amember/library/Am/Lite.php";?>
    Then where ever yo uwant to display login link:
    PHP:
    <?php  if(Am_Lite::getInstance()->isLoggedIn()) : ?>
    Welcome <?php Am_Lite::getInstance()->getName();?>
    <?php 
    else : ?>
    <a href='/amember/login'>login</a>
    <?php endif;?>
  5. ak48avk

    ak48avk Angela Kane

    Joined:
    Aug 9, 2010
    Messages:
    124
    I know I'm dabbling in areas I don't understand - I'm not sure where the 'include' line goes. I created a simple php file for testing. (I will be learning some php basics).

    I'm getting:

    Fatal error: Please move require 'Lite.php' line to the top of your file. It should be placed before any html code or php output. in /home/myusername/public_html/amember/library/Am/Lite.phpon line 874
  6. ak48avk

    ak48avk Angela Kane

    Joined:
    Aug 9, 2010
    Messages:
    124
    So, further to the above, can I just check, this code has to be in php files. So if my menu appears on all my pages then all my html files will need to be php files?

    I have read that php scripts can be placed in html files but I gather not in the case of Am/Lite.

    Sorry for complete ignorance!
  7. thehpmc

    thehpmc Member

    Joined:
    Aug 24, 2006
    Messages:
    901
    To be simplistic php can be included in otherwise html files but they must have a .php extension.

    Your file such as:
    PHP:
    <?php include "/full/path/to/amember/library/Am/Lite.php";?>
     
    <!DOCTYPE html>
     
    <html lang="en-gb">
    <head>
    Head code
    </head>
    <body>
    Body code
    </body>
    </html>
    For example 'yourpage.htm(l)' becomes 'yourpage.php'

    This is simply to instruct the server to effectively run the scripts while being fed from the server to visitors computer. Any 'htm(l)' file can be changed to a 'php' one but it does slow it down by a few milliseconds.
  8. ak48avk

    ak48avk Angela Kane

    Joined:
    Aug 9, 2010
    Messages:
    124
    It works! Thank you.

    I shall utilise more Am/Lite once I've got more familiar with php. Another great facility.
  9. hilary

    hilary aMember Pro Customer

    Joined:
    Aug 22, 2006
    Messages:
    104
    I'm just getting set up with version 4, and I have the site-wide conditional menu, too. Just realised I need to do a search and replace across the whole site to include the am/lite thing in place of the 'session start' that we had for version 3.

    I think if you have a lot of 'if own this product then display this, else display that...' going on - more than just one menu in a template file - it's probably easier to use the Wordpress integration. That's beautifully granular - control links in menus, pages, bits of pages, the works. Even control where people are sent who click links for things they don't have access to. And all point and click rather than forgetting a semi colon before the ?> and replacing the whole page with an error message... (guess how I know :confused: ...)
  10. ak48avk

    ak48avk Angela Kane

    Joined:
    Aug 9, 2010
    Messages:
    124
    Thanks Hilary. I really haven't looked into Wordpress and would love a quick summary of it's advantages and why it is used so widely with aMember. I'm probably stuck in the past with my former programming experience and I am inclined to want to do things my way!

    Yes, I have had to recreate all my pages as .php and also conscious of not ruining all the index related issues for google search. I have left my html pages up refreshing to the new .php pages until google reindexes my site. I'm sure I will be told there is a better way but I've done it now!
  11. hilary

    hilary aMember Pro Customer

    Joined:
    Aug 22, 2006
    Messages:
    104
    I can't give a complete general answer - but speaking for myself, I currently have Amember protecting a regular hard-coded site and I full intend to switch all its pages to Wordpress (when I've got my breath back from this upgrade, that is). Basically because it makes things quicker and easier: creating pages, editing pages, creating menus, updating templates, and all the content protection in Amember.

    It makes the biggest difference for people who'd never dream of editing a page as a text file. I have a friend who just recently learned to copy and paste (at least, I think she did), and since she was set up with a Wordpress site she is happily adding and editing pages, adding signup forms and so on. Not so long ago, someone like her would've been paying a webmaster for every edit.

    But even if you have no objection to editing source code, Wordpress is still going to speed things up. An example: since my site's in Europe, I really ought to have an 'accept cookies' warning message up by now. I haven't, because Googling, finding code that works and adding it to all my pages is the kind of job I prefer to avoid. You know how it'll be: code to add in the header, code to add to display the message, fiddle code to make the styling fit my site, fiddle tweak swear, run find-replace to ensure I've included everything where it needs to be, and three months later find pages I forgot all about...

    If I had a pure Wordpress site, I would grab a 'cookie compliance plugin', free or cheap, install and activate it in a couple of clicks, twiddle its settings from a nice clear control panel - all done.

    And the same goes for other 'oh, that would be good to get round to' things: social sharing buttons, elegant spam-proof contact forms... there will be a choice of plugins, with 100s of reviews and ratings from other users. No more happy hours spent scouring script sites.

    Hm. Maybe I'll make that move soon, and then sort the cookie warning. Hopefully the EU is a little too busy with Cyprus to sue me for a while.
  12. ak48avk

    ak48avk Angela Kane

    Joined:
    Aug 9, 2010
    Messages:
    124
    Further to my statement above. I realise that refreshing my pages from the old .html to .php pages isn't a good method. It is of course having a bad effect on SEO!

    I've researched as far as I can but I don't want to get it wrong!

    Is it best to do 301 permanent redirects? I would like to redirect from .html pages within folders to .php pages of the same name. Can this be done using a .htaccess file in each folder as needed or should it be at the root level? I am willing to do every page individually if necessary as my site is not so big.

    Some advice suggests RewriteRule but that looks more complicated.

    As always, assistance is most appreciated.
  13. ak48avk

    ak48avk Angela Kane

    Joined:
    Aug 9, 2010
    Messages:
    124
    I'm continuing this thread from last year as I would like to take the API Lite a little further.

    The above was a great help as all I needed was to know if visitors were signed in.

    I'm struggling with the syntax to show other features listed in the amember documentation. For example if I want to list a signed in member's products, is this right?

    <?php Am_Lite::getInstance()->getProducts() ;?>

    I'm not getting anything appearing.

    Alexander, please give me some code for dummies, like you did before! I would like to be able to have if/else code for linking depending on specific products and depending on paid or free etc.

    Also, what is the difference between <?php include "/full/path/to/amember/library/Am/Lite.php";?>

    and <?php require_once "/full/path/to/amember/library/Am/Lite.php";?>

    require_once - is in the aMember documentation.

    Thanks very much.

  14. thehpmc

    thehpmc Member

    Joined:
    Aug 24, 2006
    Messages:
    901
    Both will work if your script finds the required or included file but if there any problems then the results will differ:
    'require' will produce a fatal error and stop
    'include' will produce error but script will continue

    For your example
    PHP:
    <?php Am_Lite::getInstance()->getProducts() ;?>
    You need to tell your script what to do with the above so try
    PHP:
    <?php echo 'Am_Lite::getInstance()->getProducts()' ;?>
    the 'echo' bit tells it to print them on the screen.
  15. ak48avk

    ak48avk Angela Kane

    Joined:
    Aug 9, 2010
    Messages:
    124
    Thank you very much for this. I'm nearly getting there.

    I would like to be able to take this further. I've mentioned before I was a programmer/analyst late '60s through '70s (started very young!) and coding beyond the '80s is a bit of a mystery. Obviously a better knowledge of php is required but a few examples would help no end. PHP is a big subject.

    <?php echo Am_Lite::getInstance()->getProducts() ;?>​

    just prints 'Array'. How do I print the Array?​

    If I want an If/else searching on Product say, what is the syntax for the search parameter?​

    Say I have a product - Member Subscriber. Or do I search on Product Number. Or can I search on Product Categories.​

    I'm trying things out but it is hit and miss. Please can we have some useful examples in the aMember Documentation.

    I am finding that the

    constant Am_Lite::pAID - any not-free subscription (sorry editor keeps sticking in smiley face!)​

    is returning - PAID - even for members who have only signed up for free products/membership. I note that in the list of Invoices, payment system 'Free' gets a status of 'paid'. So does aMember consider a free payment as paid. In which case the above constant isn't very useful. I seem to remember pre V.4 that Free had a status of Free?

    I hope I can be pointed in the right direction. I can make my site a lot more responsive by exploiting API Lite - it's a shame not to use it. It's a great facility that aMember offers, I have the logic but not the up to date coding skills! I'm sure I am not alone.

    Angela




  16. alexander

    alexander Administrator Staff Member

    Joined:
    Jan 8, 2003
    Messages:
    6,279
    Angela,
    You can print an array of products the following way:
    PHP:
    <?php 
    foreach(Am_Lite::getInstance()->getProducts() as $product)
      echo 
    $product['title']."<br/>";  
    ?>
    In regards to search please let me know what exact information do you need I will show you examples.
    In some cases Am_Lite API doesn;t have necessary functions but you can use php to get required results

Share This Page