Demo website: https://2fa.hk/tools/qr/generate_qr.php
Generating QR codes in a PHP environment is entirely feasible. You can use a PHP library to help you accomplish this task. PHP QR Code is a popular library that allows you to generate QR codes in PHP.
1. Install the PHP QR Code Library
First, you need to download and install the PHP QR Code library. You can obtain it from the PHP QR Code official website or its GitHub page.
Assume you have downloaded the phpqrcode library and placed it in your project.
2. Generate a QR Code Using PHP
Create a PHP page that accepts a link and generates a QR code image. Here are the simple implementation steps:
- Install/Include the QR Code Library: Unzip phpqrcode and include the phpqrcode folder in your project.
- PHP file to generate the QR code: generate_qr.php
<?php
// Include the QR generation library
include("/www/wwwroot/根目录/vendor/phpqrcode/qrlib.php");
$defaultUrl = "https://2fa.hk";
$qrImage = '';
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$url = isset($_POST['url']) ? trim($_POST['url']) : '';
if (empty($url)) {
$error = "Please enter a valid URL!";
} elseif (!filter_var($url, FILTER_VALIDATE_URL)) {
$error = "Please enter a valid URL!";
} else {
$url = filter_var($url, FILTER_SANITIZE_URL);
$_SESSION['url'] = $url;
// Generate QR code image
$tempDir = "/www/wwwroot/根目录/tools/qr/temp_qr";
if (!file_exists($tempDir)) {
mkdir($tempDir, 0777, true);
}
$uniqueName = md5($url . time() . rand()) . ".png";
$fileName = "$tempDir/$uniqueName";
$qrImage = "/tools/qr/temp_qr/$uniqueName";
QRcode::png($url, $fileName, QR_ECLEVEL_L, 8, 2);
$_SESSION['qrImage'] = $qrImage;
// Do not redirect, display directly
// header("Location: " . $_SERVER['PHP_SELF']);
// exit();
}
} else {
$url = isset($_SESSION['url']) ? $_SESSION['url'] : $defaultUrl;
$qrImage = isset($_SESSION['qrImage']) ? $_SESSION['qrImage'] : '';
}
?>
<?php if (isset($error)) : ?>
<div id="error-message" class="error-message"><?= $error ?></div>
<?php endif; ?>
<div class="container">
<nav class="laotie">
<form action="" method="post" class="qr-form">
<h2>QR Code Generator</h2>
<div class="qr-container">
<div class="qr-left">
<label for="url">Please enter a link:</label>
<input type="text" id="url" name="url" value="<?= htmlspecialchars($url) ?>" placeholder="https://2fa.hk" required>
<button type="submit">Generate QR Code</button>
<p class="info-text">QR codes generated on this site are valid indefinitely</p>
</div>
<div class="qr-right">
<?php
$defaultQrImage = '/tools/qr/qr_code.png';
$finalImage = $qrImage ?: $defaultQrImage;
?>
<img src="<?= htmlspecialchars($finalImage) ?>" alt="QR Code" class="qr-image" />
<?php if ($qrImage): ?>
<?php endif; ?>
</div>
</div>
</form>
</nav>
</div>
<style>
.qr-form {
max-width: 1160px;
margin: 0 auto;
padding: 20px;
background-color: white;
border-radius: 8px;
}
.qr-container {
display: flex;
justify-content: space-between;
gap: 20px;
}
.qr-left, .qr-right {
flex: 1;
}
.qr-left {
display: flex;
flex-direction: column;
justify-content: center;
}
.qr-left label {
font-size: 1.2rem;
margin-bottom: 8px;
}
.qr-left input[type="text"] {
width: 100%;
padding: 10px;
margin-bottom: 20px;
border: 1px solid #ddd;
border-radius: 4px;
box-sizing: border-box;
}
.qr-left button {
width: 100%;
padding: 10px;
background-color: #4CAF50;
color: white;
border: none;
border-radius: 4px;
font-size: 1.1rem;
cursor: pointer;
}
.qr-left button:hover {
background-color: #45a049;
}
.qr-right {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
}
.qr-image {
display: block;
margin: 20px auto;
max-width: 100%;
height: auto;
}
.info-text {
margin-top: 10px;
font-size: 0.9rem;
color: #555;
text-align: center;
}
.error-message {
position: fixed;
top: 280px;
left: 50%;
transform: translateX(-50%);
background-color: #f44336;
color: white;
padding: 10px;
border-radius: 5px;
font-size: 1rem;
z-index: 9999;
opacity: 1;
transition: opacity 0.5s ease-in-out;
visibility: visible;
}
@media (max-width: 768px) {
.qr-container {
flex-direction: column;
}
}
</style>
<script>
window.onload = function() {
var errorMessage = document.getElementById('error-message');
if (errorMessage) {
setTimeout(function () {
errorMessage.style.opacity = 0;
setTimeout(function () {
errorMessage.style.visibility = 'hidden';
}, 500);
}, 5000);
}
}
</script>
3. Explanation:
- Step 1: Include the phpqrcode library at the top of the page using include.
- Step 2: Get the URL parameter from the page. The url is the link for which you want to generate a QR code. If no url parameter is passed, it defaults to https://www.example.com.
- Step 3: Use the QRcode::png() method to generate the QR code and save it to the temporary directory temp_qr.
- Step 4: Display the generated QR code image.
4. Usage:
- Place the phpqrcode library folder in your project directory.
- Place the generate_qr.php file above on your PHP server.
- Access generate_qr.php?url=your_link via a browser, for example:
http://localhost/generate_qr.php?url=https://www.example.com
- You will then see the generated QR code. Clicking the link can generate QR codes for different links.
5. Example:
- Access the link http://localhost/generate_qr.php?url=https://www.google.com, and the page will display a QR code pointing to the Google website.
In this way, you can generate a dynamic QR code page in a PHP environment. After scanning the QR code, users will be directed to the specified link.
1