📅  最后修改于: 2023-12-03 15:18:19.426000             🧑  作者: Mango
cPanel is a popular web hosting control panel that allows website owners to manage their web hosting accounts. One of the features of cPanel is that it provides an interface for managing and storing files. In this tutorial, we will look at how to use PHP to store links in cPanel.
Before we dive into the code, there are a few things that you will need.
A web hosting account with cPanel access.
Basic knowledge of PHP.
The first step is to create a database in cPanel. You can do this by logging into cPanel and selecting the "MySQL Databases" option. Here, you can create a new database and assign a username and password to it.
Once you have created the database, you can create a PHP script that will store links in the database. Here is some sample code that you can use:
<?php
// connect to the database
$dbhost = 'localhost';
$dbuser = 'username_here';
$dbpass = 'password_here';
$dbname = 'database_name_here';
$conn = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname);
// check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
// get link and store in database
$link = $_POST['link'];
$sql = "INSERT INTO links (link) VALUES ('$link')";
if (mysqli_query($conn, $sql)) {
echo "Link stored successfully";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
mysqli_close($conn);
?>
This script does the following:
Connects to the database using the provided credentials.
Retrieves the link from a form submission using the $_POST
superglobal.
Inserts the link into a table in the database.
Returns a message indicating that the link was stored successfully.
Next, you need to upload the PHP script to your web hosting account. You can do this using an FTP client or by using the file manager in cPanel. Once uploaded, make sure that the script has the correct permissions.
Finally, you need to create a HTML form that will allow users to submit links. Here is some sample code that you can use:
<form action="store_link.php" method="post">
<label for="link">Enter link:</label>
<input type="text" id="link" name="link"><br><br>
<input type="submit" value="Store Link">
</form>
This form does the following:
Specifies the action to be taken when the form is submitted. In this case, it is the PHP script created in Step 2.
Provides a input field for the user to enter the link.
Provides a submit button for the user to submit the form.
In this tutorial, we looked at how to use PHP to store links in cPanel. We created a database, wrote a PHP script to insert links into the database, uploaded the script to our web hosting account, and created a HTML form to allow users to submit links. This is just a basic example, but you can build on this to create more complex web applications.