{ Learn Code. Share Code. Teach Code. Love Code. }

  • CONTEST - Submit your PHP [CLOSED]

  • Here you will find the latest competition contest and the entries for them. Hurry and submit yours now.
Here you will find the latest competition contest and the entries for them. Hurry and submit yours now.
 #84032  by smashapps
 Thu Nov 03, 2016 9:27 am
Hey guys,

I have an AWESOME prize to give away to you folks! Get excited for this one :D

Details:

Ends 17th November 2016

I will pick a winner myself

How to enter:

Submit your favourite PHP snippets. Simple. I am doing a lot of PHP programming at the moment and it would be nice to have a few examples up my sleeve if I need it.
Any snippet you submit will not be used commercially or redistributed so don't worry.

Here's the good part!

You will receive a copy of:

Lumino City
Broken Age
Titan Souls



I have played Broken Age on PC as I bought a copy when it was released and it's a really fun, challenging game that requires you to pay a lot of attention to detail. At least I had to anyway lol

If I haven't been clear on anything just ask and I will explain myself, but all you need to do is submit your PHP snippet and have your chance to win those three games :)
Last edited by smashapps on Fri Nov 18, 2016 7:14 am, edited 1 time in total.
 #84035  by CodenStuff
 Fri Nov 04, 2016 4:28 pm
Here's one to get the Video ID from a Youtube 'watch' URL.

Example URL:
This is the code:
Code: Select all
function youtubevideoid($videourl)
{
if (preg_match('![?&]{1}v=([^&]+)!', $videourl . '&', $m))
    return $m[1];	
}
Usage:
Code: Select all
youtubevideoid("https://www.youtube.com/watch?v=IgQWRuo2D6w");
Will return:
IgQWRuo2D6w
 #84037  by AnoPem
 Fri Nov 04, 2016 11:04 pm
Heres my no database required login

I was bored one day and decided to make this login system that doesnt require any database.
It saves all userdata to a file, of own choice with encrypted names and passwords

I made it just for fun to check out some functions of php

Class: Rollover to view spoiler

Code: Select all
<?php
	class user {
		
		private $credentials_file;
		
		public function __construct($credentials_file){
			$this->credentials_file = $credentials_file;
			if(!file_exists($credentials_file)){
				file_put_contents($credentials_file, serialize(array()));
			}
		}
		function salt($length){
			$output = '';
			$i = 0;
			$chars = array_merge(range('A','Z'), range('a','z'), range('0','9'));
			$count = count($chars) - 1;
			while($i < $length){
				$output .= $chars[mt_rand(0,$count)];
				$i++;
			}
			return $output;
		}
		function encrypt($input, $key) {
			if (strlen($key) >= 33){
				$key = substr($key, 0, 32 - strlen($key));
			}
			$size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_CBC);
			$iv = mcrypt_create_iv($size, MCRYPT_RAND);
			$output = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, utf8_encode($input), MCRYPT_MODE_ECB, $iv);
			return $output;
		}
		function decrypt($input, $key) {
			if (strlen($key) >= 33){
				$key = substr($key, 0, 32 - strlen($key));
			}
			$size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_CBC);
			$iv = mcrypt_create_iv($size, MCRYPT_RAND);
			$output = mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $key, $input, MCRYPT_MODE_ECB, $iv);
			return $output;
		}
		function create($username, $password, $salt){
			$credentials_file = $this->credentials_file;
			$username = $this->encrypt($username, $salt);
			$password = $this->encrypt($password, $salt);
			$credentials = file_get_contents($credentials_file);
			$credentials = unserialize($credentials);
			if(!array_key_exists($username, $credentials)){
				$credentials[$username] = $password;
				$output = array("status" => true, "reason" => "User created");
			} else {
				$output = array("status" => true, "reason" => "User already exists");
			}
			$credentials = serialize($credentials);
			file_put_contents($credentials_file, $credentials);
			return $output;
		}
		function remove($username, $password, $salt){
			$credentials_file = $this->credentials_file;
			$username = $this->encrypt($username, $salt);
			$password = $this->encrypt($password, $salt);
			$credentials = file_get_contents($credentials_file);
			$credentials = unserialize($credentials);
			if(array_key_exists($username, $credentials)){
				if($credentials[$username] == $password){
					unset($credentials[$username]);
					$credentials = serialize($credentials);
					file_put_contents($credentials_file, $credentials);
					$output = array("status" => true, "reason" => "User succesfully removed");
				} else {
					$output = array("status" => true, "reason" => "Wrong password");
				}
			} else {
				$output = array("status" => true, "reason" => "User dosent exists");
			}
			return $output;
		}
		function login($username, $password, $salt){
			session_start();
			$credentials_file = $this->credentials_file;
			$original_username = $username;
			$username = $this->encrypt($username, $salt);
			$password = $this->encrypt($password, $salt);
			$credentials = file_get_contents($credentials_file);
			$credentials = unserialize($credentials);
			if(array_key_exists($username, $credentials)){
				if($credentials[$username] == $password){
					$_SESSION['auth'] = 2;
					$_SESSION['username'] = $original_username;
					$output = array("status" => true, "reason" => "User succesfully logged in");
				} else {
					$output = array("status" => true, "reason" => "Wrong password");
				}
			} else {
				$output = array("status" => true, "reason" => "User dosent exists");
			}
			return $output;
		}
	}
?>
[/spoiler]
Usage: Rollover to view spoiler

Code: Select all
<meta charset="UTF-8">
<?php
	require_once('classes/user.php');
	$user = new user('permission.ini');
	$salt = "TYe0Z8TEHvJElf0eHs3iUai2GjU4WCG7";
	
	/*	//Create user */
	if(isset($_GET['username']) != '' && isset($_GET['password']) != ''){
		$username = $_GET['username'];
		$password = $_GET['password'];
		
		$create = $user->create($username, $password, $salt);
		if($create['status']){
			echo $create['reason'];
		}
	}
	
	
	echo '<br>';
	
	
	/*	//User login */
	if(isset($_GET['username']) != '' && isset($_GET['password']) != ''){
		$username = $_GET['username'];
		$password = $_GET['password'];
		
		$remove = $user->login($username, $password, $salt);
		if($remove['status']){
			echo $remove['reason'];
		}
	}
	
	
	echo '<br>';
	
	
	//User remove 
	if(isset($_GET['username']) != '' && isset($_GET['password']) != ''){
		$username = $_GET['username'];
		$password = $_GET['password'];
		
		$login = $user->remove($username, $password, $salt);
		if($login['status']){
			echo $login['reason'];
		}
	}
	
	echo '<br><br><br>';
	
	@session_start();
	var_dump($_SESSION);
	
	
?>
[/spoiler]
 #84060  by smashapps
 Fri Nov 18, 2016 7:10 am
Okay guys the contest is over!

the winner is CodenStuff, congratulations. Sorry AnoPem :)