0% found this document useful (0 votes)
27 views10 pages

Unit-4 (Web Development Using PHP)

web development notes with php
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views10 pages

Unit-4 (Web Development Using PHP)

web development notes with php
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Unit-4

Topic-1: Introduction of MVC pattern models


The Model-View-Controller (MVC) pattern is a software architectural pattern
commonly used in developing user interfaces. It separates an application into
three interconnected components: the Model, the View, and the Controller. Here's
a brief introduction to each:
 Model: The Model represents the data and business logic of the
application. It encapsulates the data and provides methods to manipulate
that data. The Model responds to requests for information, and it also
notifies observers when the data changes, ensuring that the View remains
synchronized with the underlying data.
 View: The View is responsible for presenting the data to the user. It
displays the user interface elements and interacts with the user. Views are
often passive; they receive data from the Model and present it to the user
but don't directly manipulate the data. In some implementations, Views can
observe changes in the Model and update themselves accordingly.
 Controller: The Controller acts as an intermediary between the Model and
the View. It receives user input, processes it, and updates the Model
accordingly. It also updates the View in response to changes in the Model.
Controllers interpret user actions (such as button clicks or menu selections)
and translate them into commands for the Model or the View.
The key idea behind MVC is separation of concerns, which enhances
maintainability, scalability, and reusability of the codebase. By separating the
presentation logic (View) from the business logic (Model) and the user input
handling (Controller), MVC promotes a modular and organized structure for
applications.
MVC has been widely adopted in various software development frameworks,
including web development frameworks like Ruby on Rails, Django, and
ASP.NET MVC, as well as desktop application frameworks like Cocoa (for
macOS development) and JavaFX.
Topic-2: Configuration Codelgniter
CodeIgniter is a powerful PHP framework known for its simplicity, speed, and
flexibility in building web applications. Here's a basic guide to configuring
CodeIgniter:
1- Download CodeIgniter:
 Visit the CodeIgniter website and download the latest version of the
framework.
2- Extract the files:
 Once downloaded, extract the files to your local development environment
or your server.
3- Set up your web server:
 CodeIgniter works with various web servers like Apache, Nginx, etc.
Ensure your web server is correctly configured to serve PHP files.
4- Configuration files:
 CodeIgniter has a few important configuration files located in the
‘application/config’ directory. The most crucial one is ‘config.php’. This
file contains settings such as base URL, index page, encryption key, etc.
5- Database configuration:
 If your application requires database interaction, open
‘application/config/database.php’ and configure your database settings.
6- .htaccess file:
 CodeIgniter uses ‘mod_rewrite’ to create clean URLs. Ensure that
‘mod_rewrite’ is enabled on your server. Also, CodeIgniter comes with an
‘.htaccess’ file for Apache servers. If you're using Apache, make sure the
‘.htaccess’ file is in your root directory and configured properly.
7- Routing:
 CodeIgniter uses a default routing mechanism. You can define custom
routes in ‘application/config/routes.php’ to specify which controller and
method should handle a particular URI.
8- Controllers, Models, and Views:
 CodeIgniter follows the MVC (Model-View-Controller) architecture.
Place your controllers in ‘application/controllers’, models in
‘application/models’, and views in ‘application/views’.
9- Testing:
 After configuration, create a simple controller, view, and model to ensure
everything is working as expected. You can access your application via the
browser using the configured base URL.
10- Error handling and debugging:
 CodeIgniter provides robust error handling and debugging mechanisms.
Make sure to set your environment correctly in ‘index.php’ to
‘development’ for debugging purposes.
Topic-3: Setting up Codelgniter with apache
Setting up CodeIgniter with Apache involves a few steps. Here's a basic guide:
 Download CodeIgniter: First, download the latest version of CodeIgniter
from the official website: https://codeigniter.com/download.
 Extract CodeIgniter: Extract the downloaded CodeIgniter archive to a
location accessible by your Apache server. This could be your local
development environment or your server's web directory.
 Configure Apache: Ensure that Apache's mod_rewrite module is enabled.
This module is necessary for CodeIgniter's URL rewriting to work
properly.
 Configure Virtual Host (Optional): If you're setting up CodeIgniter in a
virtual host environment, create a virtual host configuration file for your
site. For example:

Replace ‘yourdomain.com’ with your domain name and


‘/path/to/your/codeigniter_project’ with the actual path to your CodeIgniter
project directory.
 Set up .htaccess: CodeIgniter uses an ‘.htaccess’ file to enable clean
URLs. Ensure that the ‘.htaccess’ file shipped with CodeIgniter is present
in your project's root directory. If it's not there, you can create one. Here's
a basic ‘.htaccess’ configuration:
 Configure CodeIgniter: CodeIgniter requires minimal configuration.
Most of the settings are stored in the ‘application/config/config.php’ file.
Ensure that you've configured your base URL and any other settings
specific to your environment.
 Test: Finally, test your setup by accessing your CodeIgniter project in a
web browser. If everything is set up correctly, you should see the default
CodeIgniter welcome page.
Topic-4: Data validation in PHP
Data validation in PHP is crucial for ensuring that the data your application
receives is accurate, secure, and consistent. Here's a basic overview of how you
can perform data validation in PHP:
1- Validate Input Data Types: Before processing any data, ensure that it
matches the expected data type. For example, if you're expecting an integer,
use functions like ‘is_int()’ or ‘filter_var()’ to check if the input is indeed
an integer.

2- Sanitize Input Data: Sanitize the input data to remove any potentially
harmful characters or code. You can use functions like ‘filter_input()’ or
‘filter_var()’ with appropriate filter options.
3- Validate Email Addresses: Use PHP's built-in ‘filter_var()’ function with
the ‘FILTER_VALIDATE_EMAIL’ filter to validate email addresses.

4- Validate Form Submissions: When dealing with form submissions, check


if required fields are filled and validate each field individually.
5- Custom Validation Rules: For complex validation rules, you can write
custom validation functions and apply them as needed.
6- Error Handling: Provide meaningful error messages to users when
validation fails, indicating what went wrong and how to correct it.
By implementing these steps, you can effectively validate user input in your PHP
applications, ensuring data integrity and security. Additionally, consider using
frameworks like Laravel or Symfony, which provide built-in mechanisms for data
validation.
Topic-5: Controller function
In PHP, a controller function typically refers to a function within the controller of
a web application that handles incoming HTTP requests, processes them, and
returns an appropriate HTTP response. Controllers are a part of the MVC (Model-
View-Controller) architecture, which helps in organizing code in a structured and
maintainable manner.
 The ‘UserController’ class contains two controller functions: ‘show()’
and ‘update()’.
 The ‘show()’ function is responsible for displaying a user's profile based
on the ‘$userId’ parameter passed through the URL.
 The ‘update()’ function handles updating user information based on data
submitted via an HTTP POST request.
 These functions can interact with models (like ‘User’ model) to retrieve or
update data, and they typically pass data to view templates for rendering
HTML output.

Here's a basic example of a controller function in PHP:

Topic-6: Controller Variables and Parameters


In PHP, controller variables and parameters typically refer to the data passed from
the client-side (usually through a form submission or URL parameters) to the
server-side PHP script for processing. These variables and parameters are then
handled by the controller, which is a part of the MVC (Model-View-Controller)
architecture commonly used in web development.
Here's a basic overview of how controller variables and parameters are used in
PHP:
 Form Submission: When a form is submitted in HTML, the data is sent
to a PHP script for processing. The form fields become variables in the
PHP script, accessible through the ‘$_POST’ or ‘$_GET’ superglobals
depending on the form submission method (‘POST’ or ‘GET’,
respectively).
 URL Parameters: Parameters can also be passed directly through the URL
using the ‘GET’ method. These parameters are accessible through the
‘$_GET’ superglobal array.
 Controller Logic: Once the variables and parameters are received in the
PHP script, the controller logic can manipulate this data, perform
validations, interact with the model layer to retrieve or store data in the
database, and finally generate the appropriate response.
 Parameterized URLs: In PHP frameworks like Laravel or Symfony,
routing systems allow defining routes with parameters, which are then
automatically passed to the controller methods.
Topic-7: Getting post data in PHP
In PHP, one can access POST data submitted from a form using the ‘$_POST’
superglobal array. Here's a basic example of how to retrieve POST data:

In this example, ‘$_POST["username"]’ is used to access the value of the input


field named "username" from the submitted form. You can replace "username"
with the name of the input field you want to retrieve from the form.
Remember, to securely handle user input, you should always validate and sanitize
any data submitted via forms to prevent security vulnerabilities like SQL injection
or cross-site scripting (XSS) attacks. You can use functions like
‘htmlspecialchars()’ or prepared statements in PDO for database interactions to
sanitize input.
Topic-8: Element and Helpers
In PHP, an "element" typically refers to an item within an array or a property of
an object. Arrays in PHP are ordered maps that allow you to store multiple values
under a single variable name. Each value in an array is accessed via a key, which
can be numeric or string-based. The term "element" is often used to refer to these
individual values within an array.
Here's a basic example of an array with elements:
$fruits = array("apple", "banana", "orange");
In this array, "apple", "banana", and "orange" are elements, and they can be
accessed using numeric indices:

PHP also provides several helper functions and language constructs to work with
arrays efficiently. Some common ones include:
 count(): Returns the number of elements in an array.
 array_push(): Adds one or more elements to the end of an array.
 array_pop(): Removes the last element from an array.
 array_shift(): Removes the first element from an array.
 array_unshift(): Adds one or more elements to the beginning of an array.
 array_slice(): Extracts a slice of an array.
 array_merge(): Merges one or more arrays.
 array_keys(): Returns all the keys or a subset of the keys of an array.
 array_values(): Returns all the values of an array.
 in_array(): Checks if a value exists in an array.
 array_search(): Searches the array for a given value and returns the
corresponding key if successful.
 foreach(): Iterates over each element in an array.
These helper functions and constructs make it easier to manipulate arrays and
access their elements in PHP.
Topic-9: Reading a session data in PHP
Reading session data in PHP is fairly straightforward. PHP manages session data
through the $_SESSION superglobal array. Here's a basic example of how you
can read session data in PHP:

In this example:
 ‘session_start()’ is used to start or resume a session.
 ‘$_SESSION['username']’ is used to access the value of the 'username'
variable stored in the session.
 The ‘isset()’ function checks if the 'username' variable is set in the session.
You can place this code at the beginning of any PHP script where you want to
access session data. Make sure to call ‘session_start()’ before attempting to
access or modify session data in any PHP script.
Topic-10: Delete data from session
To delete data from a session in PHP, you can use the unset() function to remove
the specific session variable you want to delete.
Here's how you can do it:
In this example:
 ‘unset($_SESSION['username'])’ removes the 'username' variable from
the session.
 Optionally, you can use ‘session_destroy()’ to destroy the entire session if
you want to remove all session data.
Remember to call ‘session_start()’ before trying to access or modify session
data, and ensure that the session has already been started before attempting to
delete session variables.

You might also like