The Community Forums

Interact with an entire community of cPanel & WHM users!
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Auto Create Account w/PHP

Discussion in 'cPanel Developers' started by markfrompf, Dec 27, 2006.

  1. markfrompf

    markfrompf Well-Known Member

    Joined:
    Mar 27, 2006
    Messages:
    176
    Likes Received:
    0
    Trophy Points:
    16
    Location:
    Los Angeles, CA
    Hello all. I am using the following script to try to auto create an account but it does nothing.

    What am I missing here - Something major I'm guessing?

    PHP:
    <form action="setup.php" method='post'>
    <table border="0" align="center" cellpadding="3" cellspacing="0">
      <tr>
        <td><div align="right">
          <input name="domain" type="text" id="domain">
        </div></td>
      </tr>
      <tr>
        <td><div align="right">
          <input name="pass" type="text" id="pass">
        </div></td>
      </tr>
      <tr>
        <td><div align="right">
          <input name="user" type="text" id="user">
        </div></td>
      </tr>
      <tr>
        <td><div align="right">
          <input type="submit" name="Submit" value="Submit">
        </div></td>
      </tr>
    </table></form>

    <?php

    require '/usr/local/cpanel/Cpanel/Accounting.php.inc';
    $host "localhost";
    $user "root";
    $accesshash 'The remote access key goes here, I think';
    $usessl "0";
    $acctdomain "";
    $acctplan "Business Package";

    createacct ($host,$user,$accesshash,$usessl,$acctdomain,$acctuser,$acctpass,$acctplan);

    ?>
     
  2. Spiral

    Spiral BANNED

    Joined:
    Jun 24, 2005
    Messages:
    2,023
    Likes Received:
    7
    Trophy Points:
    0
    Your basic format is good except that the way your example is written,
    you would depend on register global being enabled plus you are not
    properly setting your variable names and you made a few mistakes
    where variable names in the script are mismatched from the form.

    I would do something a little more like the following:
    Code:
    <form action="setup.php" method='post'>
    <table border="0" align="center" cellpadding="3" cellspacing="0">
      <tr>
        <td><div align="right">
          <input name="domain" type="text">
        </div></td>
      </tr>
      <tr>
        <td><div align="right">
          <input name="apass" type="text">
        </div></td>
      </tr>
      <tr>
        <td><div align="right">
          <input name="auser" type="text">
        </div></td>
      </tr>
      <tr>
        <td><div align="right">
          <input type="submit" name="Submit" value="Submit">
        </div></td>
      </tr>
    </table></form>
    
    PHP:
    <?php
    require '/usr/local/cpanel/Cpanel/Accounting.php.inc';

    foreach(
    $_POST as $key => $val) {
      $
    $key strtolower(trim(stripslashes($val)));
    }

    $host "localhost";
    $user "root";
    $rkey '(paste your remote access key here)';
    $plan "(paste your plan name here)";

    @
    createacct("$host""$user""$rkey"0"$domain""$auser""$apass""$plan");
    Note that you originally named your plan "Business Package" which is a bad name!

    Names of account packages should not contain any spaces or punctuation
    which the sole exception being the underscore (_) character which is permitted.

    The variables used in your "createacct" function should match the names of
    the respective variables used in your form. In your original example, you had
    different variable names for the username and password for your form and
    for the createacct function in your PHP script which is your main problem here.

    Because of the "require" line calling a file outside the web path, you will need
    to either disable "open_basedir" for the hosting account using the setup script
    or you will need to add the /usr/local/cpanel/Cpanel path to open_basedir
    in the /etc/httpd/conf/httpd.conf configuration file.

    Naturally you can greatly expand upon this sample code and add in sanity checks
    and test code to make sure the proper variables have been set but, this is
    the basics of what you need to know here.
     
    #2 Spiral, Dec 27, 2006
    Last edited: Dec 27, 2006
  3. markfrompf

    markfrompf Well-Known Member

    Joined:
    Mar 27, 2006
    Messages:
    176
    Likes Received:
    0
    Trophy Points:
    16
    Location:
    Los Angeles, CA
    Excellent. Works great. Is there a way to set the nameservers using this script?

    It sets up the account but for some reason with the wrong nameserver (auth20.ns.wcom.com).

    I know my NS's are configured correctly in the WHM on this server, so is this "auth20.ns.wcom.com" in the config on the file this script is requiring?
    EDIT: I just looked in the file /usr/local/cpanel/Cpanel/Accounting.php.inc and there isn't anything in there like that.

    Where do you think that NS is coming from?
     
    #3 markfrompf, Dec 28, 2006
    Last edited: Dec 28, 2006
  4. Spiral

    Spiral BANNED

    Joined:
    Jun 24, 2005
    Messages:
    2,023
    Likes Received:
    7
    Trophy Points:
    0
    Using the root hash in your scripts, the DNS servers you want to use as the default
    would be the ones specfied in WHM's "Basic cPanel/WHM Setup".

    Also under "Tweak Settings" the following option should NOT be checked:
    Code:
    [i]"When adding a new domain, if the domain is already registered, ignore the configured nameservers, and set the NS line to the authoritative (registered) ones."[/i]
    Things are a little bit different for resellers ....

    For resellers, their default DNS servers are configured in the "Reseller Center".
     
  5. markfrompf

    markfrompf Well-Known Member

    Joined:
    Mar 27, 2006
    Messages:
    176
    Likes Received:
    0
    Trophy Points:
    16
    Location:
    Los Angeles, CA
    Ok, got it. Thank you very much. It's because the test domain was already registered. :eek:

    Thank you very much for your help!
     
  6. markfrompf

    markfrompf Well-Known Member

    Joined:
    Mar 27, 2006
    Messages:
    176
    Likes Received:
    0
    Trophy Points:
    16
    Location:
    Los Angeles, CA
    Sorry to ask again, but what would be a way to list accounts with this script?

    Thanks in advance.
     
  7. Spiral

    Spiral BANNED

    Joined:
    Jun 24, 2005
    Messages:
    2,023
    Likes Received:
    7
    Trophy Points:
    0

    Yes, there is a function in the API to list account ....

    Code:
    $accounts = listaccts ($host,$user,$accesshash,$usessl);
    
    $host -- usually localhost

    $user -- 'root' or whatever reseller user you are using

    $accesshash -- same hash as the account create function

    $usessl --- if using SSL then '1', else set to '0'


    Aside from the API, another simple way for getting a list of usernames
    is just call "ls /var/cpanel/users" on the system and pipe the results back.
     
  8. markfrompf

    markfrompf Well-Known Member

    Joined:
    Mar 27, 2006
    Messages:
    176
    Likes Received:
    0
    Trophy Points:
    16
    Location:
    Los Angeles, CA
    Thank you very much. You truly are a guru! :)
     
  9. jameshsi

    jameshsi Well-Known Member

    Joined:
    Oct 22, 2001
    Messages:
    347
    Likes Received:
    0
    Trophy Points:
    16
    If I want to list all accounts on remote server, can i use this too ?
     
  10. markfrompf

    markfrompf Well-Known Member

    Joined:
    Mar 27, 2006
    Messages:
    176
    Likes Received:
    0
    Trophy Points:
    16
    Location:
    Los Angeles, CA
    That's the one to use, jameshsi.
     
  11. jameshsi

    jameshsi Well-Known Member

    Joined:
    Oct 22, 2001
    Messages:
    347
    Likes Received:
    0
    Trophy Points:
    16
    Hi!
    Thanks for your reply,markfrompf, but what I want is to list a remote server's accounts, not localhost, it should be able to do that if you put the remote server's IP to $host , right, but the truth is, you can only list all accounts on the same server your scripts reside, am I right about this ?
     
  12. markfrompf

    markfrompf Well-Known Member

    Joined:
    Mar 27, 2006
    Messages:
    176
    Likes Received:
    0
    Trophy Points:
    16
    Location:
    Los Angeles, CA
    Not necessarily, but quite possibly. I'd definitely give it a shot.

    :) Good luck! Please let us know about the outcome of this.
     
  13. EstebanC

    EstebanC Member

    Joined:
    Jul 1, 2007
    Messages:
    9
    Likes Received:
    0
    Trophy Points:
    1
    Hello, I am using the example provided by Spiral but the page setup.php just stay in blank.

    What is my error?

    thanks


     
  14. EstebanC

    EstebanC Member

    Joined:
    Jul 1, 2007
    Messages:
    9
    Likes Received:
    0
    Trophy Points:
    1
    Solved!

    the problem was that I must set the "username" with 8 characters and setup a stronger password ;)

    Now, I only need to know which is the "email" contact variable, I tried with "acctcontact" but it didn't work.

    Any help? :$

    Thanks!
     
    #14 EstebanC, Mar 11, 2008
    Last edited: Mar 11, 2008
  15. Ryan88

    Ryan88 Registered

    Joined:
    Mar 6, 2011
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    1
    Hate to bump a thread from 2008 but does anyone know how to add a e-mail variable to this?
     
  16. MrLeN

    MrLeN Active Member

    Joined:
    Dec 12, 2010
    Messages:
    38
    Likes Received:
    0
    Trophy Points:
    6
    I have set this up also, but once I run the script, it just shows setup.php as a blank white page.

    Then, when I go into WHM to see if the account was created, it wasn't.

    I realise this thread is old (I got here from a Google search).

    I am using cPanel 11.30.5

    Any ideas?
     
    #16 MrLeN, Jan 25, 2012
    Last edited: Jan 25, 2012
  17. MrLeN

    MrLeN Active Member

    Joined:
    Dec 12, 2010
    Messages:
    38
    Likes Received:
    0
    Trophy Points:
    6
    I am getting sick and tired :(

    How hard can it possibly be to find a script on the Internet that allows php to add an account to WHM.

    There's scripts everywhere, but most of them are old and don't work because now cpanel requires a key.

    And any new script I can find, just plain doesn't work.

    ie: http://forums.cpanel.net/f42/create-account-cpanel-php-251911.html

    Why can't I find this anywhere?
     
  18. cPanelDavidG

    cPanelDavidG Technical Product Specialist

    Joined:
    Nov 29, 2006
    Messages:
    11,279
    Likes Received:
    8
    Trophy Points:
    38
    Location:
    Houston, TX
    cPanel Access Level:
    Root Administrator
    cPanel has changed much in 6 years, and this post predates most of the APIs currently available in the product. Those looking for more information and resources can now reference cPanel & WHM's SDK
     
  19. cPanelDavidG

    cPanelDavidG Technical Product Specialist

    Joined:
    Nov 29, 2006
    Messages:
    11,279
    Likes Received:
    8
    Trophy Points:
    38
    Location:
    Houston, TX
    cPanel Access Level:
    Root Administrator
    Actually there are several scripts in production use based off that PHP class on that thread. Furthermore, we do not require a key, that is every bit as optional as it was back in 2006. However, it is generally considered good practice to use a key.

    Many scripts from over 6 years ago no longer work due to them relying on kludges that were in place prior to us officially supporting those functionalities via API as well as new security functionality introduced over the years such as CSRF protection which older scripts have a tendency to not accommodate well. This is why we recommend use of the APIs (instead of workaround kludges) as it helps ensure scripts that work today will continue to work for years into the future.
     
  20. MrLeN

    MrLeN Active Member

    Joined:
    Dec 12, 2010
    Messages:
    38
    Likes Received:
    0
    Trophy Points:
    6
    Can you point me to the part that explains how to create a new WHM account with PHP?
     
Loading...

Share This Page