How to execute PHP script when user signs up

Discussion in 'Customization & add-ons' started by alexblyz91, Oct 10, 2019.

  1. alexblyz91

    alexblyz91 New Member

    Joined:
    Aug 9, 2019
    Messages:
    24
    I need to execute a php script once when a new user signs up, which file do i need to edit?
  2. caesar

    caesar aMember Pro Developer Staff Member

    Joined:
    Oct 16, 2009
    Messages:
    2,295
    Hello,

    You can put the following code snippet to site.php file:
    http://www.amember.com/docs/Site.php_file

    PHP:
    Am_Di::getInstance()->hook->add(Am_Event::USER_AFTER_INSERT, function (Am_Event $e) {
        
    /** @var User $user */
        
    $user $e->getUser();

        
    //execute any code, you can relay on $user (represent record from user table)
    });
  3. alexblyz91

    alexblyz91 New Member

    Joined:
    Aug 9, 2019
    Messages:
    24
    Thanks, how can i echo the username of the user that has signed up?
  4. alexblyz91

    alexblyz91 New Member

    Joined:
    Aug 9, 2019
    Messages:
    24
    I've tried this, but when a user signs up it echoes a blank line instead of the username:

    PHP:
    <?php
    require_once '/var/www/html/members/library/Am/Lite.php';
    $username=Am_Lite::getInstance()->getUsername();
    if (!
    defined('INCLUDED_AMEMBER_CONFIG'))
        die(
    "Direct access to this location is not allowed");
    Am_Di::getInstance()->hook->add(Am_Event::USER_AFTER_INSERT, function (Am_Event $e) {
        
    /** @var User $user */
     
        
    exec("echo '".$username."' >> /tmp/test");
    });
  5. caesar

    caesar aMember Pro Developer Staff Member

    Joined:
    Oct 16, 2009
    Messages:
    2,295
    I do not recommend to use Am_Lite within aMember codebase (this class is designed to access data about logged in user from 3rd party scripts).

    In your case you need to use $user->login ie.:
    PHP:
     Am_Di::getInstance()->hook->add(Am_Event::USER_AFTER_INSERT, function (Am_Event $e) {
        
    /** @var User $user */
        
    $user $e->getUser();
        
    $arg escapeshellarg($user->login);
        
    exec("echo {$arg} >> /tmp/test");
    }); 

Share This Page