PHP
PHP
web development. Initially created in 1994 by Rasmus Lerdorf, PHP is designed to be embedded within
HTML, enabling developers to add dynamic content, interact with databases, handle form data, and
manage sessions. PHP is widely known for its ease of use, extensive community support, and versatility
across platforms and server configurations. Here are some key concepts and features that make PHP
integral to web development:
1. **Server-Side Scripting**:
- PHP code runs on the server, generating HTML content sent to the client’s browser. This approach
means that end-users cannot view PHP code, only the generated HTML.
- It allows for building dynamic web pages, where content can change based on user actions, time, or
database entries.
2. **Database Integration**:
- PHP has built-in support for various databases, such as MySQL, PostgreSQL, and SQLite. Using PHP,
developers can connect to databases, perform CRUD (Create, Read, Update, Delete) operations, and
display dynamic data from databases on web pages.
- PHP’s syntax resembles C, Java, and Perl, making it relatively easy to learn for those familiar with
these languages. PHP code is enclosed within `<?php ... ?>` tags, making it easy to integrate with HTML.
- Basic syntax includes variables (preceded by `$`), control structures (like `if`, `for`, `while`), and
functions.
4. **Error Handling**:
- PHP includes built-in error-handling functions, allowing developers to manage runtime errors
efficiently, making debugging easier.
5. **Form Handling**:
- PHP is commonly used for handling HTML form data. Using the `$_POST` and `$_GET` superglobals,
PHP can collect and process user input.
6. **Sessions and Cookies**:
- PHP can manage user sessions, allowing data to be stored across different pages on a website. It can
also set cookies on the user's browser to store information.
7. **PHP Frameworks**:
- PHP has a rich ecosystem of frameworks like Laravel, Symfony, and CodeIgniter. These frameworks
provide tools, libraries, and structured architecture for building complex web applications more
efficiently.
Here’s a simple PHP script that outputs “Hello, World!” to demonstrate PHP syntax.
<?php
?>
```
PHP is ideal for web applications that require interaction with a database, form processing, or user
management systems.
If you want to create a basic login system without using a database, you can store user credentials
directly within the PHP code. This approach is generally suitable only for very basic or testing purposes,
as it lacks the security and flexibility of a database-backed system.
This form collects the username and password from the user.
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Login</title>
</head>
<body>
<h2>Login</h2>
<label for="username">Username:</label>
<label for="password">Password:</label>
<button type="submit">Login</button>
</form>
</body>
</html>
```
In this step, we’ll check the submitted credentials against predefined values stored in the PHP code. If
the credentials are correct, a session will be started, and the user will be redirected to a welcome page.
If not, an error message will be displayed.
```php
<?php
session_start();
$correct_username = 'testuser';
$correct_password = 'testpassword';
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$username = $_POST['username'];
$password = $_POST['password'];
$_SESSION['username'] = $username;
exit();
} else {
?>
```
The welcome page will only be accessible if the user is logged in (i.e., a session is active).
```php
<?php
session_start();
if (!isset($_SESSION['username'])) {
exit();
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Welcome</title>
</head>
<body>
<a href="logout.php">Logout</a>
</body>
</html>
```
### Step 4: Create a Logout Script (logout.php)
This script destroys the session, effectively logging the user out.
```php
<?php
session_start();
exit();
?>
```
### Summary
- `login.php` - Contains the login form and processes the login attempt.
This setup will provide basic login functionality without a database, with credentials stored directly in
PHP. For more security and scalability, a database-based login system is recommended.