Thursday, December 8, 2022

Create a simple game using php

It is not possible to create a game using PHP alone because PHP is a server-side programming language and is not designed for creating user interfaces or displaying graphics. To create a game, you would need to use a technology that is capable of rendering graphics and handling user input, such as a graphics library or a game engine.

However, you can use PHP to create the backend for a game, such as a web service that stores game data or a server that handles game logic and communication between players. For example, you could create a multiplayer online game where the game client runs on the user's device and communicates with a PHP server to retrieve game data and send player actions.

Here is an example of how you could use PHP to create a simple game backend:

<?php // The game data $game_data = [ 'score' => 0, 'lives' => 3, 'level' => 1, ]; // The game actions $game_actions = [ 'move_left' => function() { // Move the player to the left }, 'move_right' => function() { // Move the player to the right }, 'shoot' => function() { // Shoot a bullet }, ]; // Handle a game action $action = $_POST['action']; if ( isset( $game_actions[ $action ] ) ) { $game_actions[ $action ](); } // Return the game data echo json_encode( $game_data );

This PHP code defines a simple game with a score, lives, and level, and defines a few actions that the player can perform. The code receives an action from the game client, performs the action, and returns the updated game data. This is just a simple example to show how you could use PHP to create a game backend. You can modify this code and add more game logic and behavior to create a more complex game.

Wednesday, August 24, 2022

Gitlab upload local files to remote repository

Hello, You can use the followiung method to upload locl files to remote repository in Gitlab cd existing_folder git init --initial-branch=main git remote add origin git@gitlab.com:youraccount/project_name.git git add . git commit -m "Initial commit" git push -u origin main

Wednesday, October 31, 2018

Transfer file remotely (Server to Server)

Transfer file remotely (Server to Server)

<?php
define('BUFSIZ', 4095);
$url = 'http://www.remoteserveraddress.com/test.zip';
$rfile = fopen($url, 'r');
$lfile = fopen(basename($url), 'w');
while(!feof($rfile))
fwrite($lfile, fread($rfile, BUFSIZ), BUFSIZ);
fclose($rfile);
fclose($lfile);
?>


Step 1: First you will need to add a file called remoteupload.php where you want to upload the files.
Paste the above code in that file.
Step 2: Replace the url with your url required to be remotely upload.
Step 3 : Run the file remoteupload.php and it will automatically copy your requested files in a fraction of second.
Hope this is helpful for you.



Tuesday, November 26, 2013

Basics of MVC

The model view controller pattern is the most used pattern for today’s world web applications. It has been used for the first time in Smalltalk and then adopted and popularized by Java. At present there are more than a dozen PHP web frameworks based on MVC pattern.

Despite the fact that the MVC pattern is very popular in PHP, is hard to find a proper tutorial accompanied by a simple source code example. That is the purpose of this tutorial.

The MVC pattern separates an application in 3 modules:

 Model, View and Controller:

The model is responsible to manage the data; it stores and retrieves entities used by an application, usually from a database, and contains the logic implemented by the application.


The view (presentation) is responsible to display the data provided by the model in a specific format. It has a similar usage with the template modules present in some popular web applications, like wordpress, joomla,


The controller handles the model and view layers to work together. The controller receives a request from the client, invokes the model to perform the requested operations and sends the data to the View. The view formats the data to be presented to the user, in a web application as an html output.

Example to add watermark to an image

example to add watermark to an image:

here stamp.png is real image and photo.jpeg would be the generated image havng watermark.
<?php
// Load the stamp and the photo to apply the watermark to
$stamp = imagecreatefrompng('stamp.png');
$im = imagecreatefromjpeg('photo.jpeg');

// Set the margins for the stamp and get the height/width of the stamp image
$marge_right = 10;
$marge_bottom = 10;
$sx = imagesx($stamp);
$sy = imagesy($stamp);

// Copy the stamp image onto our photo using the margin offsets and the photo 
// width to calculate positioning of the stamp. 
imagecopy($im, $stamp, imagesx($im) - $sx - $marge_right, imagesy($im) - $sy - $marge_bottom, 0, 0, imagesx($stamp), imagesy($stamp));

// Output and free memory
header('Content-type: image/png');
imagepng($im);
imagedestroy($im);
?>

Function to generate random code

Here is a sample function that generates random code,it can be used for generating encryption codes or passwords etc.

<?php
function gen_code($total_length = 8, $use_caps = true, $use_numeric = true, $use_specials = true) {
$capsall = array();
$nums = array();
$nums_specials = 0;
$reglength = $total_length;
$tws = array();
for ($ch = 97; $ch <= 122; $ch++) $chars[] = $ch; // create a-z
if ($use_caps) for ($ca = 65; $ca <= 90; $ca++) $capsall[] = $ca; // create A-Z
if ($use_numeric) for ($nu = 48; $nu <= 57; $nu++) $nums[] = $nu; // create 0-9
$all = array_merge($chars, $capsall, $nums);
if ($use_specials) {
$reglength =  ceil($total_length*0.75);
$nums_specials = $total_length - $reglength;
if ($nums_specials > 5) $nums_specials = 5;
for ($si = 33; $si <= 47; $si++) $signs[] = $si;
$rs_keys = array_rand($signs, $nums_specials);
foreach ($rs_keys as $rs) {
$tws[] = chr($signs[$rs]);
}
}
$rand_keys = array_rand($all, $reglength);
foreach ($rand_keys as $rand) {
$tw[] = chr($all[$rand]);
}
$compl_str = array_merge($tw, $tws);
shuffle($compl_str);
return implode('', $compl_str);
}
echo gen_code(10);
?>

Sunday, November 6, 2011

Simple method to start your php

Hi all,
i am going to give you a little thingy on how to use php in windows using ('zend framework') and xampp.I used dreamweaver(personaly i prefer dreamweaver but you may use even a notepad to write php code) .

I am first going to show you the connection detail to MySQl..

This is the common used connection script for any php::

mysql_connection("hostname","username","password");

after establising conection::use the following code to select your required database:

mysql_select_db('databasename');

These two step is the commonly used steps to establish and manipulate mysql server.

Remember to save your file in xampp directory (usually c:/ drive) in any computer.