V8 Javascript Engine Integration

Giriş

This extension embeds the » V8 Javascript Engine into PHP.

add a note

User Contributed Notes 3 notes

up
0
jquery at gmail dot com
13 hours ago
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Mini jQuery To-Do</title>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
    <style>
        .completed { text-decoration: line-through; color: gray; }
        li { margin-bottom: 5px; cursor: pointer; }
        button { margin-left: 10px; color: red; }
    </style>
</head>
<body>

    <h2>Mini To-Do List</h2>
    <input type="text" id="taskInput" placeholder="New task...">
    <button id="addBtn">Add</button>

    <ul id="taskList"></ul>

<script>
$(function() {
    
    // Standard .click() function on the main button
    $('#addBtn').click(function() {
        let text = $('#taskInput').val().trim(); // Get input value
        if (text === '') return; // If empty, do nothing

        // Create elements in memory
        let li = $('<li>').text(text);
        let deleteBtn = $('<button>').text('Delete');

        // BINDING .click() FUNCTIONS DIRECTLY TO NEW ELEMENTS:
        
        // Clicking the task text toggles the strikethrough
        li.click(function() {
            $(this).toggleClass('completed');
        });

        // Clicking the delete button removes the task
        deleteBtn.click(function(e) {
            e.stopPropagation(); // Prevents the click from bubbling up and crossing out the text (li.click)
            li.remove(); // Smazání elementu / Deletes the element
        });

        // Assemble the elements and append them to the DOM
        li.append(deleteBtn);
        $('#taskList').append(li);

        $('#taskInput').val(''); // Clear the input field
    });

});
</script>

</body>
</html>
up
0
gemik850 at gmail dot com
13 hours ago
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Triangle Calculator</title>
    <style>
        body { font-family: sans-serif; margin: 40px; }
        .container { max-width: 400px; padding: 20px; border: 1px solid #ccc; border-radius: 8px; }
        input { width: 100%; padding: 8px; margin: 10px 0; box-sizing: border-box; }
        button { padding: 10px 15px; background-color: #007BFF; color: white; border: none; cursor: pointer; border-radius: 4px; }
        button:hover { background-color: #0056b3; }
        .result { margin-top: 20px; font-weight: bold; color: #333; }
    </style>
</head>
<body>

<div class="container">
    <h2>Right-Angled Triangle Calculator</h2>
    
    <label for="sideA">Length of side A (cm):</label>
    <input type="number" id="sideA" placeholder="Enter a number..." min="0.1" step="any">

    <label for="sideB">Length of side B (cm):</label>
    <input type="number" id="sideB" placeholder="Enter a number..." min="0.1" step="any">

    <button onclick="calculateTriangle()">Calculate</button>

    <div id="resultOutput" class="result"></div>
</div>

<script>
    // Define the function that runs when the button is clicked
    function calculateTriangle() {
        // 1. Get values from HTML inputs and convert them to decimals (floats)
        let a = parseFloat(document.getElementById('sideA').value);
        let b = parseFloat(document.getElementById('sideB').value);
        let resultDiv = document.getElementById('resultOutput');

        // 2. Validation: Check if the user entered valid, positive numbers
        if (isNaN(a) || isNaN(b) || a <= 0 || b <= 0) {
            resultDiv.innerHTML = "❌ Please enter valid, positive numbers for both sides.";
            resultDiv.style.color = "red";
            return; // Stops the function execution if the data is invalid
        }

        // 3. MATHEMATICAL OPERATIONS AND FUNCTIONS
        
        // Calculate the area (Basic operations: multiplication and division)
        // Formula: Area = (a * b) / 2
        let area = (a * b) / 2;

        // Calculate the hypotenuse C (Pythagorean theorem: c^2 = a^2 + b^2)
        // Using Math.pow() for squaring and Math.sqrt() for the square root
        let cSquared = Math.pow(a, 2) + Math.pow(b, 2);
        let hypotenuseC = Math.sqrt(cSquared);

        // Using Math.round() to round the result (e.g., to 2 decimal places)
        // Multiplying and dividing by 100 shifts the decimal point before rounding, then shifts it back
        let roundedHypotenuse = Math.round(hypotenuseC * 100) / 100;

        // 4. Output the result back to the HTML
        resultDiv.style.color = "green";
        resultDiv.innerHTML = 
            "✅ Calculation successful:<br><br>" +
            "Triangle Area: " + area + " cm²<br>" +
            "Length of Hypotenuse C: " + roundedHypotenuse + " cm";
    }
</script>

</body>
</html>
up
0
gemik850 at gmail dot com
13 hours ago
PHP
<?php
// 1. Session and Database Initialization
session_start();

$host = 'localhost';
$db   = 'database_name';
$user = 'username';
$pass = 'password';

$dsn = "mysql:host=$host;dbname=$db;charset=utf8mb4";
$options = [
    PDO::ATTR_ERRMODE            => PDO::ERRMODE_EXCEPTION,
    PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
];

try {
     $pdo = new PDO($dsn, $user, $pass, $options);
} catch (\PDOException $e) {
     throw new \PDOException($e->getMessage(), (int)$e->getCode());
}

// 2. Authentication Logic (Login)
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action_login'])) {
    $email = trim($_POST['email']);
    $password = $_POST['password'];

    $stmt = $pdo->prepare('SELECT id, password_hash FROM users WHERE email = ?');
    $stmt->execute([$email]);
    $user = $stmt->fetch();

    if ($user && password_verify($password, $user['password_hash'])) {
        // Regenerate session ID to prevent session fixation attacks
        session_regenerate_id(true);
        
        $_SESSION['auth_user_id'] = $user['id'];
        $_SESSION['auth_logged_in'] = true;
        
        header('Location: dashboard.php');
        exit;
    } else {
        echo "Invalid email or password.";
    }
}

// 3. Verification Condition (Check if user is logged in)
if (!isset($_SESSION['auth_logged_in']) || $_SESSION['auth_logged_in'] !== true) {
    // User is not authenticated, redirect to login page
    header('Location: login.php');
    exit;
}

// 4. Accessible only to authenticated users
echo "Access granted. User ID: " . htmlspecialchars($_SESSION['auth_user_id']);
?>
To Top