E Commerce Lab Practicals

Download as pdf or txt
Download as pdf or txt
You are on page 1of 17

1. Home page design of web site code?

<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="UTF-8">

<meta name="viewport" content="width=device-width, initial-scale=1.0">

<title>My Website</title>

<link rel="stylesheet" href="styles.css">

</head>

<body>

<header>

<h1>Welcome to My Website</h1>

<nav>

<ul>

<li><a href="#home">Home</a></li>

<li><a href="#about">About</a></li>

<li><a href="#services">Services</a></li>

<li><a href="#contact">Contact</a></li>

</ul>

</nav>

</header>

<main>

<section id="home">

<h2>Home Section</h2>

<p>This is the home section of the website. Welcome!</p>


</section>

<section id="about">

<h2>About Us</h2>

<p>Learn more about us here.</p>

</section>

<section id="services">

<h2>Our Services</h2>

<p>Details about services offered.</p>

</section>

<section id="contact">

<h2>Contact Us</h2>

<p>Get in touch with us!</p>

</section>

</main>

<footer>

<p>&copy; 2024 My Website. All rights reserved.</p>

</footer>

</body>

</html>

2.Validation using PHP code?


1. Basic Form Validation

Here's a simple example of how to validate a form with PHP:

<?php

// Initialize variables to store error messages

$nameErr = $emailErr = "";


$name = $email = "";

// Process the form when it's submitted

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

// Validate Name

if (empty($_POST["name"])) {

$nameErr = "Name is required";

} else {

$name = sanitize_input($_POST["name"]);

if (!preg_match("/^[a-zA-Z-' ]*$/", $name)) {

$nameErr = "Only letters and white space allowed";

// Validate Email

if (empty($_POST["email"])) {

$emailErr = "Email is required";

} else {

$email = sanitize_input($_POST["email"]);

if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {

$emailErr = "Invalid email format";

// Function to sanitize user input

function sanitize_input($data) {
return htmlspecialchars(stripslashes(trim($data)));

?>

<!DOCTYPE html>

<html>

<head>

<title>Form Validation Example</title>

</head>

<body>

<h2>PHP Form Validation Example</h2>

<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">

Name: <input type="text" name="name">

<span style="color: red;"><?php echo $nameErr;?></span>

<br><br>

Email: <input type="text" name="email">

<span style="color: red;"><?php echo $emailErr;?></span>

<br><br>

<input type="submit" name="submit" value="Submit">

</form>

</body>

</html>

2. Validating a Password

You can also implement password validation to ensure that users create strong passwords:
<?php

$passwordErr = "";

$password = "";

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

if (empty($_POST["password"])) {

$passwordErr = "Password is required";

} else {

$password = sanitize_input($_POST["password"]);

if (strlen($password) < 8 || !preg_match("/[A-Za-z]/", $password) || !preg_match("/[0-9]/",


$password)) {

$passwordErr = "Password must be at least 8 characters long and include letters and
numbers";

function sanitize_input($data) {

return htmlspecialchars(stripslashes(trim($data)));

?>

<!-- Add to your form -->

Password: <input type="password" name="password">

<span style="color: red;"><?php echo $passwordErr;?></span>

<br><br>

3.implement catalog design code?


<?php

// Sample data for the catalog

$products = [

'id' => 1,

'name' => 'Product 1',

'description' => 'This is a great product.',

'price' => 19.99,

'image' => 'https://via.placeholder.com/150'

],

'id' => 2,

'name' => 'Product 2',

'description' => 'This is another great product.',

'price' => 29.99,

'image' => 'https://via.placeholder.com/150'

],

'id' => 3,

'name' => 'Product 3',

'description' => 'This is yet another great product.',

'price' => 39.99,

'image' => 'https://via.placeholder.com/150'

];

?>
<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="UTF-8">

<meta name="viewport" content="width=device-width, initial-scale=1.0">

<link rel="stylesheet" href="style.css">

<title>Product Catalog</title>

</head>

<body>

<div class="container">

<h1>Product Catalog</h1>

<div class="catalog">

<?php foreach ($products as $product): ?>

<div class="product">

<img src="<?php echo $product['image']; ?>" alt="<?php echo $product['name']; ?>">

<h2><?php echo $product['name']; ?></h2>

<p><?php echo $product['description']; ?></p>

<p class="price">$<?php echo number_format($product['price'], 2); ?></p>

<button>Add to Cart</button>

</div>

<?php endforeach; ?>

</div>

</div>
</body>

</html>

4. Implement access control mechanism(eg:user name and password)?


Here’s a complete implementation of a simple access control mechanism in PHP using username
and password authentication. This setup includes a login form, authentication logic, a protected
dashboard, and a logout option.

Directory Structure

1. login.php - The login form.

2. authenticate.php - The script to process login attempts.

3. dashboard.php - A protected page for logged-in users.

4. logout.php - Script to log out users

Step 1: Create login.php

<?php

session_start();

?>

<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="UTF-8">

<meta name="viewport" content="width=device-width, initial-scale=1.0">

<link rel="stylesheet" href="style.css">

<title>Login</title>

</head>

<body>
<div class="container">

<h2>Login</h2>

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

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

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

<br><br>

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

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

<br><br>

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

</form>

<?php

if (isset($_SESSION['error'])) {

echo "<p style='color: red;'>".$_SESSION['error']."</p>";

unset($_SESSION['error']);

?>

</div>

</body>

</html>

Step 2: Create authenticate.php

<?php

session_start();
// Sample user credentials (for demonstration only; use hashed passwords in production)

$valid_username = "admin";

$valid_password = "password123"; // Store hashed passwords in production

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

$username = $_POST['username'];

$password = $_POST['password'];

// Simple authentication check

if ($username === $valid_username && $password === $valid_password) {

$_SESSION['loggedin'] = true;

$_SESSION['username'] = $username;

header("Location: dashboard.php");

exit();

} else {

$_SESSION['error'] = "Invalid username or password.";

header("Location: login.php");

exit();

Step 3: Create dashboard.php

<?php

session_start();

// Check if the user is logged in

if (!isset($_SESSION['loggedin']) || $_SESSION['loggedin'] !== true) {


header("Location: login.php");

exit();

?>

<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="UTF-8">

<meta name="viewport" content="width=device-width, initial-scale=1.0">

<link rel="stylesheet" href="style.css">

<title>Dashboard</title>

</head>

<body>

<div class="container">

<h2>Welcome, <?php echo $_SESSION['username']; ?>!</h2>

<p>This is a protected area accessible only to logged-in users.</p>

<a href="logout.php">Logout</a>

</div>

</body>

</html>

Step 4: Create logout.php:

<?php

session_start();
session_unset();

session_destroy();

header("Location: login.php");

exit();

5.case study on business model of online e commerce store code?


Creating a case study for an online e-commerce store involves several components, such as
business model description, key functionalities, technology stack, and a basic implementation of
features. Below, I’ll outline a simple case study and provide example code snippets that represent
the essential parts of an e-commerce platform.

Case Study: E-Commerce Store

Business Model

Business Model

1. Overview:

o An online platform where customers can browse and purchase a variety of products, such as
electronics, clothing, and home goods.

o Revenue generated through product sales, possibly supplemented by advertisements and affiliate
marketing.

2. Target Market:

o Tech-savvy individuals aged 18-45 who prefer shopping online for convenience and variety.

3. Value Proposition:

 Wide selection of products, competitive pricing, convenient shopping experience, and home
delivery.

 Revenue Streams:

 Product sales

 Shipping fees

 Affiliate marketing

 Advertisements
5.cost Structure:

o Website development and maintenance

o Inventory management

o Marketing and advertising

o Payment processing fees

Key Functionalities

1. User Registration and Login: Users can create accounts to track orders and manage personal
information.

2. Product Catalog: A display of products categorized for easy navigation.

3. Shopping Cart: Users can add products to their cart and proceed to checkout.

4. Order Management: Users can view their order history and order status.

5. Payment Integration: Allow users to make payments via credit/debit cards or third-party payment
gateways.

6. Admin Panel: For managing products, orders, and users.

Technology Stack

 Frontend: HTML, CSS, JavaScript (with frameworks like React or Vue.js)

 Backend: PHP (with a framework like Laravel) or Node.js

 Database: MySQL or MongoDB

 Payment Gateway: Stripe or PayPal

Example Code Snippets

Here are some example code snippets for key functionalities in PHP.

1. User Registration (register.php)

<?php

session_start();

require 'database.php'; // Assume a database connection file

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

$username = $_POST['username'];
$password = password_hash($_POST['password'], PASSWORD_DEFAULT); // Hash
password for security

// Insert user into the database

$stmt = $pdo->prepare("INSERT INTO users (username, password) VALUES (?, ?)");

if ($stmt->execute([$username, $password])) {

$_SESSION['success'] = "Registration successful!";

header("Location: login.php");

exit();

} else {

$_SESSION['error'] = "Registration failed!";

?>

<!DOCTYPE html>

<html lang="en">

<head>

<title>Register</title>

</head>

<body>

<form method="post" action="">

<input type="text" name="username" required placeholder="Username">

<input type="password" name="password" required placeholder="Password">

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

</form>

</body>

</html>
<?php

session_start();

require 'database.php'; // Database connection

$stmt = $pdo->query("SELECT * FROM products");

$products = $stmt->fetchAll();

?>

2. Product Catalog (catalog.php)

<!DOCTYPE html>

<html lang="en">

<head>

<title>Product Catalog</title>

</head>

<body>

<h1>Product Catalog</h1>

<div class="product-list">

<?php foreach ($products as $product): ?>

<div class="product">

<h2><?php echo htmlspecialchars($product['name']); ?></h2>

<p><?php echo htmlspecialchars($product['description']); ?></p>

<p>Price: $<?php echo htmlspecialchars($product['price']); ?></p>

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

<input type="hidden" name="product_id" value="<?php echo $product['id']; ?>">

<button type="submit">Add to Cart</button>

</form>

</div>

<?php endforeach; ?>


</div>

</body>

</html>

3. Shopping Cart (cart.php)

<?php

session_start();

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

$_SESSION['cart'] = [];

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

$product_id = $_POST['product_id'];

$_SESSION['cart'][] = $product_id; // Add product to cart

?>

<!DOCTYPE html>

<html lang="en">

<head>

<title>Your Cart</title>

</head>

<body>

<h1>Your Cart</h1>

<ul>

<?php foreach ($_SESSION['cart'] as $item): ?>

<li>Product ID: <?php echo htmlspecialchars($item); ?></li>

<?php endforeach; ?>

</ul>
<a href="checkout.php">Proceed to Checkout</a>

</body>

</html>

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy