0% found this document useful (0 votes)
4 views

PHP

PHP is an open-source server-side scripting language primarily used for web development, allowing developers to create dynamic web pages and interact with databases. Key features include server-side scripting, database integration, error handling, and session management, along with a variety of frameworks to enhance development. A basic example demonstrates a simple login system without a database, highlighting PHP's ease of use and functionality.

Uploaded by

Kj Ortiz
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

PHP

PHP is an open-source server-side scripting language primarily used for web development, allowing developers to create dynamic web pages and interact with databases. Key features include server-side scripting, database integration, error handling, and session management, along with a variety of frameworks to enhance development. A basic example demonstrates a simple login system without a database, highlighting PHP's ease of use and functionality.

Uploaded by

Kj Ortiz
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

PHP (Hypertext Preprocessor) is a popular open-source server-side scripting language used mainly for

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:

### Key Concepts and Features

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.

3. **Syntax and Structure**:

- 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.

### Basic Example

Here’s a simple PHP script that outputs “Hello, World!” to demonstrate PHP syntax.

<?php

// This is a single-line comment in PHP

echo "Hello, World!";

?>

```

### Where to Use 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.

Here’s a simple example of a PHP login system without a database:

### Step 1: Define the Login Form (login.php)

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>

<form action="login.php" method="post">

<label for="username">Username:</label>

<input type="text" name="username" id="username" required><br><br>

<label for="password">Password:</label>

<input type="password" name="password" id="password" required><br><br>

<button type="submit">Login</button>

</form>

</body>

</html>

```

### Step 2: Process Login and Check Credentials (login.php)

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();

// Predefined username and password (for demo purposes only)

$correct_username = 'testuser';

$correct_password = 'testpassword';

// Check if the form is submitted

if ($_SERVER["REQUEST_METHOD"] == "POST") {

// Get the submitted username and password

$username = $_POST['username'];

$password = $_POST['password'];

// Verify the credentials

if ($username === $correct_username && $password === $correct_password) {

// Correct login, start the session

$_SESSION['username'] = $username;

header("Location: welcome.php"); // Redirect to welcome page

exit();

} else {

// Incorrect credentials, show an error

echo "Invalid username or password.";

?>

```

### Step 3: Create the Welcome Page (welcome.php)

The welcome page will only be accessible if the user is logged in (i.e., a session is active).
```php

<?php

session_start();

// Check if the user is logged in

if (!isset($_SESSION['username'])) {

header("Location: login.php"); // Redirect to login page if not logged in

exit();

?>

<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="UTF-8">

<title>Welcome</title>

</head>

<body>

<h1>Welcome, <?php echo htmlspecialchars($_SESSION['username']); ?>!</h1>

<p>You are successfully logged in.</p>

<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();

session_unset(); // Remove all session variables

session_destroy(); // Destroy the session

header("Location: login.php"); // Redirect to login page

exit();

?>

```

### Summary

- `login.php` - Contains the login form and processes the login attempt.

- `welcome.php` - Displays a welcome message for logged-in users.

- `logout.php` - Ends the session, logging the user out.

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.

You might also like