localhost/test
http://localhost/test
The localhost/test path is used for test pages, debug scripts, and development testing. Create a test directory to safely experiment without affecting production code.
→ Open localhost/test
Create Test Directory
# Windows XAMPP
cd C:\xampp\htdocs
mkdir test
# Linux
cd /var/www/html
sudo mkdir test
sudo chown -R www-data:www-data test
# Mac MAMP
cd /Applications/MAMP/htdocs
mkdir test
Common Test Files
PHP Info Test
Create test/phpinfo.php:
<?php
// Display PHP configuration
phpinfo();
?>
Access: localhost/test/phpinfo.php
Database Connection Test
Create test/dbtest.php:
<?php
// MySQL connection test
$host = 'localhost';
$user = 'root';
$pass = '';
$db = 'test';
$conn = mysqli_connect($host, $user, $pass, $db);
if ($conn) {
echo "Database connected successfully!";
echo "<br>Server: " . mysqli_get_server_info($conn);
mysqli_close($conn);
} else {
echo "Connection failed: " . mysqli_connect_error();
}
?>
PDO Connection Test
<?php
try {
$pdo = new PDO(
"mysql:host=localhost;dbname=test",
"root",
""
);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "PDO connection successful!";
// Test query
$stmt = $pdo->query("SELECT VERSION()");
$version = $stmt->fetch();
echo "<br>MySQL Version: " . $version[0];
} catch(PDOException $e) {
echo "Connection failed: " . $e->getMessage();
}
?>
Server Variables Test
Create test/server.php:
<?php
echo "<h2>Server Information</h2>";
echo "Server Software: " . $_SERVER['SERVER_SOFTWARE'] . "<br>";
echo "Server Name: " . $_SERVER['SERVER_NAME'] . "<br>";
echo "Server Port: " . $_SERVER['SERVER_PORT'] . "<br>";
echo "Document Root: " . $_SERVER['DOCUMENT_ROOT'] . "<br>";
echo "PHP Version: " . phpversion() . "<br>";
echo "OS: " . PHP_OS . "<br>";
echo "<h2>PHP Modules</h2>";
print_r(get_loaded_extensions());
?>
HTML Test Page
Create test/index.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Test Page</title>
<style>
body { font-family: Arial, sans-serif; padding: 20px; }
.success { color: green; }
.error { color: red; }
</style>
</head>
<body>
<h1>Test Page Working</h1>
<p class="success">If you see this, your server is running correctly.</p>
<h2>Quick Tests</h2>
<ul>
<li><a href="phpinfo.php">PHP Info</a></li>
<li><a href="dbtest.php">Database Test</a></li>
<li><a href="server.php">Server Info</a></li>
</ul>
</body>
</html>
JavaScript Test
Create test/script.html:
<!DOCTYPE html>
<html>
<head>
<title>JavaScript Test</title>
</head>
<body>
<h1>JavaScript Test</h1>
<button onclick="testFunction()">Click to Test</button>
<div id="output"></div>
<script>
function testFunction() {
document.getElementById('output').innerHTML =
'<p style="color: green;">JavaScript is working!</p>';
}
// Test on load
console.log('Page loaded successfully');
console.log('Browser:', navigator.userAgent);
</script>
</body>
</html>
File Upload Test
Create test/upload.php:
<!DOCTYPE html>
<html>
<body>
<h2>File Upload Test</h2>
<form method="post" enctype="multipart/form-data">
<input type="file" name="testfile">
<input type="submit" value="Upload">
</form>
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_FILES['testfile'])) {
$file = $_FILES['testfile'];
echo "<h3>File Information:</h3>";
echo "Name: " . $file['name'] . "<br>";
echo "Type: " . $file['type'] . "<br>";
echo "Size: " . $file['size'] . " bytes<br>";
echo "Temp: " . $file['tmp_name'] . "<br>";
echo "Error: " . $file['error'] . "<br>";
if ($file['error'] === 0) {
echo "<p style='color: green;'>Upload successful!</p>";
} else {
echo "<p style='color: red;'>Upload failed!</p>";
}
}
?>
</body>
</html>
Session Test
Create test/session.php:
<?php
session_start();
// Set session variable
if (!isset($_SESSION['visits'])) {
$_SESSION['visits'] = 0;
}
$_SESSION['visits']++;
echo "<h2>Session Test</h2>";
echo "Session ID: " . session_id() . "<br>";
echo "Page visits: " . $_SESSION['visits'] . "<br>";
echo "<br><a href=''>Refresh to increment</a>";
// Display all session variables
echo "<h3>Session Variables:</h3>";
echo "<pre>";
print_r($_SESSION);
echo "</pre>";
?>
API Test
Create test/api.php:
<?php
header('Content-Type: application/json');
// Simple API endpoint for testing
$response = [
'status' => 'success',
'message' => 'API is working',
'timestamp' => time(),
'server' => $_SERVER['SERVER_NAME'],
'method' => $_SERVER['REQUEST_METHOD']
];
echo json_encode($response, JSON_PRETTY_PRINT);
?>
Test with: curl http://localhost/test/api.php
Email Test
Create test/email.php:
<?php
// Test if mail() function works
$to = "test@example.com";
$subject = "Test Email";
$message = "This is a test email from localhost.";
$headers = "From: noreply@localhost";
$result = mail($to, $subject, $message, $headers);
if ($result) {
echo "Email function is available and executed.";
echo "<br>Note: Email may not actually send without SMTP configuration.";
} else {
echo "Email function failed or is not configured.";
}
// Check mail configuration
echo "<h3>Mail Configuration:</h3>";
echo "SMTP: " . ini_get('SMTP') . "<br>";
echo "smtp_port: " . ini_get('smtp_port') . "<br>";
echo "sendmail_path: " . ini_get('sendmail_path') . "<br>";
?>
Test Directory Structure
htdocs/test/
├── index.html # Main test page
├── phpinfo.php # PHP configuration
├── dbtest.php # Database test
├── server.php # Server info
├── script.html # JavaScript test
├── upload.php # File upload test
├── session.php # Session test
├── api.php # API endpoint test
├── email.php # Email test
└── README.txt # Test documentation
Security Considerations
- Delete on Production - Never deploy test files to production servers
- Restrict Access - Use .htaccess to password protect test directory
- No Sensitive Data - Don't use real credentials in test files
- Remove phpinfo() - Exposes server configuration details
Protect Test Directory
Create test/.htaccess:
AuthType Basic
AuthName "Test Area"
AuthUserFile /path/to/.htpasswd
Require valid-user
# Or deny all access
# Deny from all
Best Practice:
Use descriptive file names for tests. Instead of test.php, use db-connection-test.php or api-endpoint-test.php for clarity.
Common Test URLs
localhost/test/ - Main test directory
localhost/test/phpinfo.php - PHP configuration
localhost/test/dbtest.php - Database connection
localhost/test/api.php - API endpoint
Frequently Asked Questions
Why use localhost/test?
It provides an isolated location for debugging and testing without affecting your main application or production files.
Is localhost/test secure?
By default, it's accessible to anyone on localhost. For network access, use password protection or IP restrictions via .htaccess.
Should I commit test files to version control?
Generally no. Add /test/ to .gitignore to avoid committing test files, especially those with phpinfo() or database credentials.