Please whitelist cPanel in your adblocker so that you’re able to see our version release promotions, thanks!

The Community Forums

Interact with an entire community of cPanel & WHM users!

How to check if subdomain exists using php?

Discussion in 'cPanel Developers' started by shido, May 8, 2018.

  1. shido

    shido Registered

    Joined:
    Oct 8, 2008
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    51
    I am trying to check if a subdomain already exists using php. I am trying to read the etc/localdomains file using php but it says the file dosnt exist ?

    Code:
    $domain = "mysub.mydomain.com";
    $filename = '/etc/localdomains';
     if( strpos(file_get_contents('/etc/localdomains'),$domain) !== false) {
          
    
       echo "Subdomain already taken.. Please choose another";
    } else {
     
    echo "<p>Your sub domain name</p>
    <p id='domready'>".$domain."</p>
    
    <p>is available!!</p> }

    Any help would be great!
     
  2. sparek-3

    sparek-3 Well-Known Member

    Joined:
    Aug 10, 2002
    Messages:
    1,781
    Likes Received:
    123
    Trophy Points:
    343
    cPanel Access Level:
    Root Administrator
    There are some syntax errors in your script - you need to add a "; before the closing } bracket in the else statement.

    Are you sure that mysub.mydomain.com exists in /etc/localdomains?

    cat /etc/localdomains | grep mysub.mydomain.com

    Of course, this is also going to match if notmysub.mydomain.com exists in /etc/localdomains
     
  3. cPanelLauren

    cPanelLauren Forums Analyst II
    Staff Member

    Joined:
    Nov 14, 2017
    Messages:
    3,552
    Likes Received:
    253
    Trophy Points:
    193
    Location:
    Houston
    cPanel Access Level:
    DataCenter Provider
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
Loading...

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice