PHP code example of viragrajput / http-foundation

1. Go to this page and download the library: Download viragrajput/http-foundation library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

viragrajput / http-foundation example snippets


use Virag\HttpFoundation\Cookie;

// Set a cookie
Cookie::set('username', 'john_doe', time() + 3600, '/', 'example.com', true, true);

// Get a cookie value
$username = Cookie::get('username');

// Delete a cookie
Cookie::delete('username');

use Virag\HttpFoundation\Session;

// Start a session
$session = new Session();

// Set session data
$session->set('user_id', 123);

// Get session data
$userID = $session->get('user_id');

// Regenerate session ID
$session->regenerateId();

use Virag\HttpFoundation\UploadedFile;

// Handle file upload
$file = new UploadedFile(
    $_FILES['file']['name'],
    $_FILES['file']['type'],
    $_FILES['file']['tmp_name'],
    $_FILES['file']['error'],
    $_FILES['file']['size']
);

// Move uploaded file to destination directory
$file->move('uploads/');

use Virag\HttpFoundation\Response;

// Create a response with JSON content
$response = new Response();
$response->setJsonResponse(['message' => 'Success'], 200);
$response->send();

// Create a response with HTML content
$htmlContent = "<html><body><h1>Hello, World!</h1></body></html>";
$response = new Response();
$response->setHtmlResponse($htmlContent, 200);
$response->send();

// Create a redirect response
$response = new Response();
$response->setRedirectResponse('/new-page', 302);
$response->send();

// Create a response with custom headers
$response = new Response();
$response->setHtmlResponse('Custom Content', 200);
$response->setHeader('X-Custom-Header', 'Value');
$response->send();

// Create a response with an error message
$response = new Response();
$response->setBadRequestResponse('Bad Request');
$response->send();

use Virag\HttpFoundation\Request;

// Create a request object from global variables
$request = Request::createFromGlobals();

// Now you can access various properties and methods of the $request object
$method = $request->getMethod();
$uri = $request->getUri();
$params = $request->getParameters();
$headers = $request->getHeaders();