Dynadot .com transfer sale, only $10.12 with coupon code DYNA12, available while supplies last, ends October 31, 2026.

PHP question

Namecheap AuctionsNamecheap Auctions
SpaceshipSpaceship
SpaceshipSpaceship
Watch

Simran

New Member
Impact
0
Hello,
How we create Subdomains using PHP?

Thanks :$:
 
0
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
GoDaddyGoDaddy
I am not sure I understand your question but normally you don't use PHP but the webserver (e.g. apache) to create subdomains, either using virtual hosts or (.htaccess) rewriterules.
 
0
•••
php is incapable of creating subdomains unless of course you are using an api for a control panel such as cPanel.

If you want PHP to be able to do things and treat content differently depending on the subdomain used you need to do the following:-

add *.yourdomain.com as an alias in httpd.conf
add a wildcard dns entry for your domain into your dns server (most likely bind)
then use code such as:-

PHP:
<?PHP
$domain = explode('.', $_SERVER['HTTP_HOST']);
$subdomain = $domain[0];
?>

$subdomain will then contain the subdomain used to access the page.
 
0
•••
use this script to add new subdoamin in your cpanel account

subdomainform.php


<html>
<head>
<title>create a subdomain</title>
</head>
<body>
<form name="form1" method="post" action="addsubdomain.php">
<h1>create Sub Domains</h1>
Sub Domain Name : <input type="text" name="subdomain"><br />
<input type="submit" name="Submit" value="Submit">
</form>
</body>
</html>


==============================

addsubdomain.php
=============================

<?php

ini_set('display_errors', 1);

$host = "domainname.com"; // your domain name without the www
$port = 2082;
$path = "/frontend/x/subdomain/doadddomain.html?domain=".$_POST['subdomain']."&rootdomain=".$host; //or .dll, etc. for authnet, etc.

// these lines are changed
$cpaneluser = "username";
$cpanelpass = "pass";
$authstr = "$cpaneluser:$cpanelpass";
//****************************

// Setup the Auth String
$pass = base64_encode($authstr);

$fp = fsockopen($host, $port, $errno, $errstr, $timeout = 30);

if(!$fp){
//error tell us
echo "$errstr ($errno)n";

}else{

//send the server request

fputs($fp, "POST $path HTTP/1.1rn");
fputs($fp, "Host: $hostrn");
fputs($fp, "Authorization: Basic $pass rn");
fputs($fp, "Content-type: application/x-www-form-urlencodedrn");
fputs($fp, "Content-length: ".strlen($poststring)."rn");
fputs($fp, "Connection: closernrn");
fputs($fp, $poststring . "rnrn");

//*************************************
// Remove this to stop it from displaying the output fron the CPanel
//*************************************
//loop through the response from the server
/*
while(!feof($fp)) {
echo fgets($fp, 4096);
}

*/
while(!feof($fp)) { fgets($fp, 4096); }
//close fp - we are done with it
fclose($fp);
}
 
0
•••
I believe Peter answered this question correctly by how the author phrased things, I believe he wanted to know about wildcard DNS.

Make sure you have you host configure apache to have wildcard DNS run for you
 
0
•••
DomainEasy: flexible LTO options. Close more domain deals with financing.

We're social

Escrow.com
Spaceship
Escrowly
CryptoExchange.com
Domain Recover

NamePros updates

Parking: find your domains by pasting a list
You can now paste a list of domain names into the search box at the top of the Parking menu. Put one name on each line. Your domain list...
AIVikings
Catchy
  • The sidebar remains visible by scrolling at a speed relative to the page’s height.
Back