Add exams from exam156 to exam199

This commit is contained in:
2024-05-16 10:51:54 +02:00
parent 19bf1b8ac8
commit 959a167e9d
29 changed files with 635 additions and 0 deletions

37
exam156/generate.php Normal file
View File

@@ -0,0 +1,37 @@
<?php
header('Content-type: image/jpeg');
$mysql_host = 'localhost';
$mysql_user = 'example_user';
$mysql_pass = 'Kolosnjaj4321!';
$mysql_db = 'example_database';
$con = mysqli_connect($mysql_host, $mysql_user, $mysql_pass);
mysqli_select_db($con, $mysql_db);
if (isset($_GET['id'])) {
$id = $_GET['id'];
$query = mysqli_query($con, "SELECT `email` FROM `users` WHERE `id` ='" . mysqli_real_escape_string($con, $id) . "'");
if (mysqli_num_rows($query) >= 1) {
$query_result = mysqli_fetch_assoc($query);
$email = $query_result['email'];
} else {
$email = 'ID not found';
}
} else {
$email = 'No id specified.';
}
$email_length = strlen($email);
$font_size = 4;
$image_height = imagefontheight($font_size);
$image_width = imagefontwidth($font_size) * $email_length;
$image = imagecreate($image_width, $image_height); // Stvaranje prazne slike
imagecolorallocate($image, 255, 255, 255); // boja pozadine podesena na belo
$font_color = imagecolorallocate($image, 0, 0, 0); // boja fonta podesena na belo
imagestring($image, $font_size, 0, 0, $email, $font_color);
imagejpeg($image);

4
exam156/index.php Normal file
View File

@@ -0,0 +1,4 @@
Name:<br>
Alex <br><br>
Email:<br>
<img src="generate.php?id=2">

29
exam161/generate.php Executable file
View File

@@ -0,0 +1,29 @@
<?php
header('Content-type: image/jpeg');
if(isset($_GET['source'])){
$source = $_GET['source'];
$watermark = imagecreatefrompng('php.png');
$watermark_height = imagesx($watermark);
$watermark_width = imagesy($watermark); // imagesx uzima sirinu ucitane slike
// Ova funkcija stvara praznu sliku s određenom širinom i visinom, koristeći prave boje umesto indeksiranih
$image = imagecreatetruecolor($watermark_width, $watermark_height);
$image = imagecreatefromjpeg($source);
$image_size = getimagesize($source);
$x = $image_size[0] - $watermark_width - 10;
$y = $image_size[1] - $watermark_height - 10;
imagecopymerge($image,$watermark,$x,$y, 0, 0,$watermark_width,$watermark_height, 40);
imagejpeg($image); // ili imagejpeg($image, $source.'.watermark.jp'); sto nam pravi sliku u folderu ako stavimo drugi parametar
} else {
echo 'No source suplied';
}
?>

1
exam161/index.php Executable file
View File

@@ -0,0 +1 @@
<img src="generate.php?source=phone2.jpg" />

BIN
exam161/phone2.jpg Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 122 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 116 KiB

BIN
exam161/php.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 15 KiB

Binary file not shown.

31
exam164/generate.php Executable file
View File

@@ -0,0 +1,31 @@
<?php
session_start();
header('Content-type: image/jpeg');
$text = $_SESSION['secure'];
$font_size = 30;
$image_width = 120;
$image_height = 40;
$image = imagecreate($image_width,$image_height);
imagecolorallocate($image, 255, 255, 255);
$text_color = imagecolorallocate($image, 0, 0, 0);
for ($x = 1; $x <= 30; $x++) {
$x1 = rand(1,100);
$y1 = rand(1,100);
$x2 = rand(1,100);
$y2 = rand(1,100);
imageline($image,$x1,$y1,$x2,$y2,$text_color);
}
$font_filename = '/var/www/nikola/practice/exam164/HappySwirly-KVB7l.ttf'; // mora cela putanja
imagettftext($image, $font_size, 0, 15, 30,$text_color,$font_filename,$text);
imagejpeg($image);
?>

27
exam164/index.php Executable file
View File

@@ -0,0 +1,27 @@
<?php
session_start();
if(!isset($_POST['secure'])){
$_SESSION['secure'] = rand(1000, 9999);
}else {
if($_POST['secure'] == $_SESSION['secure']){
echo 'You are not robot';
} else {
echo 'Try again';
$_SESSION['secure'] = rand(1000, 9999);
}
}
?>
<br>
<img src="generate.php"/><br>
<form action="index.php" method="POST">
Write a number from picture:
<input type="text" size="6"name="secure">
<input type="submit" value="Submit">
</form>

25
exam168/index.php Normal file
View File

@@ -0,0 +1,25 @@
<?php
header('Content-type: image/jpeg');
if(isset($_GET['image'])){
$image = $_GET['image'];
$image_size = getimagesize($image);
$image_width = $image_size[0];
$image_height = $image_size[1];
$new_size = ($image_width + $image_height) / ($image_width * ($image_height / 45));
$new_width = $image_width * $new_size;
$new_height = $image_height * $new_size;
$new_image = imagecreatetruecolor($new_width, $new_height);
$old_image = imagecreatefromjpeg($image);
imagecopyresized($new_image,$old_image,0,0,0,0,$new_width,$new_height,$image_width,$image_height);
imagejpeg($new_image);
}
?>

BIN
exam168/phone2.jpg Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 122 KiB

1
exam172/include.inc.php Normal file
View File

@@ -0,0 +1 @@
Hello!

45
exam172/index.php Normal file
View File

@@ -0,0 +1,45 @@
<!DOCTYPE html>
<html lang="en">
<head>
<script type="text/javascript">
//AJAX code
function load(thediv, thefile){
//Ovde proveravamo da li browser podrzava objekat XMLHttpRequest, ako podrzava pravimo nov objekat, ako ne podrzava
//tada se koristi alternativni pristup stvaranju objekta kroz ActiveXObject.
if (window.XMLHttpRequest){
xml_http = new XMLHttpRequest();
} else {
xml_http = new ActiveXObject('Microsoft.XMLHTTP');
}
//kada se stanje objekta xml_http promeni funk ce se aktivirati
//funkcija proverava da li je stanje objekta dostiglo readyState=4 sto znaci da je zahtev zavrsen
//i da li je status HTTP odgovora 200 = 'OK'
//responseText ili odgovor se postavlja kao html sadrzaj
xml_http.onreadystatechange = function() {
if(xml_http.readyState == 4 && xml_http.status == 200){
//mi kacimo sadrzaj u <div>
document.getElementById(thediv).innerHTML = xml_http.responseText;
}
}
//open salje HTTP zahtev, prvi parametar je metoda, drugi URL adresa na koju se salje, treci true znaci da se izvrsava asihrono
//send samo za slanje http zahteva na server
xml_http.open('GET',thefile, true);
xml_http.send();
}
</script>
</head>
<body>
<input type="submit" onclick="load('anotherdiv','include.inc.php');">
<div id="anotherdiv"></div>
</body>
</html>

38
exam175/index.php Normal file
View File

@@ -0,0 +1,38 @@
<!DOCTYPE html>
<html lang="en">
<head>
<script type="text/javascript">
function findmatch(){
if(window.XMLHttpRequest){
xmlhttp =new XMLHttpRequest();
} else {
xmlhttp = new ActiveXObject('Microsoft.XMLHTTP');
}
xmlhttp.onreadystatechange = function(){
if(xmlhttp.readyState == 4 && xmlhttp.status == 200){
document.getElementById('results').innerHTML = xmlhttp.responseText;
}
}
xmlhttp.open('GET','search.inc.php?search_text=' + document.search.search_text.value ,true);
xmlhttp.send();
}
</script>
</head>
<body>
<form id="search" name="search">
Type a name: <br>
<input type="text" name="search_text" onkeyup="findmatch();">
</form>
<div id="results"></div>
</body>
</html>

26
exam175/search.inc.php Normal file
View File

@@ -0,0 +1,26 @@
<?php
$mysql_db = 'example_database';
$mysql_host = 'localhost';
$mysql_user = 'example_user';
$mysql_pass = 'Kolosnjaj4321!';
if(isset($_GET['search_text'])){
$search_text = $_GET['search_text'];
}
if(!empty($search_text)){
$con = mysqli_connect($mysql_host,$mysql_user,$mysql_pass);
if(mysqli_select_db($con,$mysql_db)){
$query = "SELECT `name` FROM `names` WHERE `name` LIKE '". mysqli_real_escape_string($con,$search_text) ."%'";
$query_run = mysqli_query($con,$query);
while($query_row = mysqli_fetch_assoc($query_run)){
echo $name = $query_row['name'] . '<br>';
}
}
}
?>

41
exam180/index.php Normal file
View File

@@ -0,0 +1,41 @@
<!DOCTYPE html>
<html lang="en">
<head>
<script type="text/javascript">
function insert(){
if(window.XMLHttpRequest){
xmlhttp = new XMLHttpRequest();
} else {
xmlhttp = new ActiveXObject('Microsoft.XMLHTTP');
}
xmlhttp.onreadystatechange = function(){
if(xmlhttp.readyState == 4 && xmlhttp.status == 200){
document.getElementById('message').innerHTML = xmlhttp.responseText;
}
}
parameters = 'text=' + document.getElementById('text').value;
xmlhttp.open('POST', 'update.inc.php', true);
xmlhttp.setRequestHeader('Content-type','application/x-www-form-urlencoded'); // slicno kao header() u php- u
xmlhttp.send(parameters);
}
</script>
</head>
<body>
Insert data: <input type="text" id="text">
<input type="button" value="Submit" onclick="insert();">
<div id="message"></div>
</body>
</html>

25
exam180/update.inc.php Normal file
View File

@@ -0,0 +1,25 @@
<?php
if (isset($_POST['text'])) {
$text = $_POST['text'];
}
$mysql_db = 'example_database';
$mysql_host = 'localhost';
$mysql_user = 'example_user';
$mysql_pass = 'Kolosnjaj4321!';
if (isset($_POST['text'])) {
$text = $_POST['text'];
if (!empty($text)) {
$con = mysqli_connect($mysql_host, $mysql_user, $mysql_pass);
if (mysqli_select_db($con, $mysql_db)) {
$query = "INSERT INTO `data` VALUES(NULL,'" . mysqli_real_escape_string($con, $text) . "')";
if ($query_run = mysqli_query($con, $query)) {
echo 'You are successfuly insert data in database';
}
} else {
echo 'not connected';
}
}
}

12
exam185/throwEx.php Normal file
View File

@@ -0,0 +1,12 @@
<?php
function divide($num1, $num2){
if($num2 == 0){
throw new Exception('Cannot devide by 0');
} else {
return $num1/$num2;
}
}
echo divide(10,0);
?>

17
exam185/trycatch.php Normal file
View File

@@ -0,0 +1,17 @@
<?php
$age = 17;
try {
if ($age > 18){
echo 'Old enough.';
} else {
throw new Exception('Not old enough');
}
} catch(Exception $ex){
echo 'Error: '. $ex -> getMessage();
}
?>

View File

@@ -0,0 +1,35 @@
<?php
$mysql_db = 'example_database';
$mysql_host = 'localhost';
$mysql_user = 'example_user';
$mysql_pass = 'Kolosnjaj4321!';
class DatabaseException extends Exception
{
public function showSpecific()
{
return 'Error thrown on line' . $this->getLine() . ' in ' . $this->getFile();
}
}
class ServerException extends Exception
{
public function showSpecific()
{
return 'Error thrown on line' . $this->getLine() . ' in ' . $this->getFile();
}
}
try {
if (!@$con = mysqli_connect($mysql_host, $mysql_user, $mysql_pass)) {
throw new ServerException();
} else if (!@mysqli_select_db($con, $mysql_db)) {
throw new DatabaseException();
} else {
echo 'Connected';
}
} catch (ServerException $ex) {
echo $ex->showSpecific();
} catch (DatabaseException $ex) {
echo $ex->showSpecific();
}

27
exam187/customEx.php Normal file
View File

@@ -0,0 +1,27 @@
<?php
// Ovo mi ne radi ne znam iz kog razloga uvek baci error kad pogresim umesto da ispise poruku.
$mysql_db = 'example_database';
$mysql_host = 'localhosta';
$mysql_user = 'example_user';
$mysql_pass = 'Kolosnjaj4321!';
class DatabaseException extends Exception{}
class ServerException extends Exception{}
try{
if(!$con = mysqli_connect($mysql_host,$mysql_user,$mysql_pass)){
throw new ServerException('Could not connect to server.');
} else if (!mysqli_select_db($con,$mysql_db)) {
throw new DatabaseException('Could not select database.');
} else {
echo 'Connected';
}
} catch (ServerException $ex) {
echo 'Error: ' . $ex->getMessage();
} catch (DatabaseException $ex) {
echo 'Error: ' . $ex->getMessage();
}
?>

31
exam190/index.php Normal file
View File

@@ -0,0 +1,31 @@
<?php
class BankAccount
{
// modifikatori vidljivosti u PHP public, protected, private
public $balance = 42;
public function DisplayBalance()
{
return 'Balance: ' . $this->balance . '<br>'; // Ovako pristupamo vrednosti balance 'pravimo referencu'
}
public function Withdraw($amount)
{
if ($this->balance < $amount) {
echo 'Not enough money <br> ';
} else {
$this->balance = $this->balance - $amount;
}
}
}
//nova instanca klase
$ba = new BankAccount;
echo $ba->DisplayBalance();
$ba->Withdraw(50);
echo $ba->DisplayBalance();

22
exam193/index.php Normal file
View File

@@ -0,0 +1,22 @@
<?php
class BankAcount
{
//ako stavimo protected ili private moci cemo da prikazemo promenljivu balance preko metoda.
private $balance = 20;
//ako stavimo protected ili private ne mozemo da pozovemo method DisplayBalance();
public function DisplayBalance()
{
return $this->balance;
}
}
$alex = new BankAcount;
echo $alex->DisplayBalance();
?>

22
exam194/constants.php Normal file
View File

@@ -0,0 +1,22 @@
<?php
//Area of circle
class Circle{
public const pi = 3.14159265359;
public function CircleArea($r){
return self::pi * $r *$r;
}
}
$area = new Circle;
echo 'if pi is '.Circle::pi . '<br>';
echo 'Area of circle is ' . $area->CircleArea(3);
?>

19
exam194/constructor.php Normal file
View File

@@ -0,0 +1,19 @@
<?php
class Example{
public function __construct($something)
{
$this-> SaySomething($something);
}
public function SaySomething($something){
echo $something;
}
}
$example = new Example('Some text');
?>

43
exam196/index.php Normal file
View File

@@ -0,0 +1,43 @@
<?php
class BankAccount
{
// modifikatori vidljivosti u PHP public, protected, private
public $balance = 0;
public function DisplayBalance()
{
return 'Balance: ' . $this->balance . '<br>'; // Ovako pristupamo vrednosti balance 'pravimo referencu'
}
public function Withdraw($amount)
{
if ($this->balance < $amount) {
echo 'Not enough money <br> ';
} else {
$this->balance = $this->balance - $amount;
}
}
public function Deposit($amount){
$this->balance = $this->balance + $amount;
}
}
//Multiple instances of class :
$alex = new BankAccount;
$billy = new BankAccount;
$alex -> Deposit(100);
$billy -> Deposit(50);
$alex -> Withdraw(98);
$billy -> Withdraw(12.5);
echo $alex -> DisplayBalance();
echo $billy -> DisplayBalance();
?>

49
exam197/index.php Normal file
View File

@@ -0,0 +1,49 @@
<?php
class BankAccount
{
public $balance = 0;
public $type = '';
public function SetType($input){
$this->type = $input;
}
public function DisplayBalance()
{
return 'Balance: ' . $this->balance . '<br>';
}
public function Withdraw($amount)
{
if ($this->balance < $amount) {
echo 'Not enough money <br> ';
} else {
$this->balance = $this->balance - $amount;
}
}
public function Deposit($amount){
$this->balance = $this->balance + $amount;
}
}
class SavingsAccount extends BankAccount {
}
$alex = new BankAccount;
$alex->SetType('18-25 Current');
$alex->Deposit(100);
$alex->Withdraw(20);
$alex_savings = new SavingsAccount;
$alex_savings -> Deposit(40);
$alex_savings->SetType('Super Saver');
echo $alex-> type .' has ' .$alex->DisplayBalance();
echo $alex_savings->type .' has ' .$alex_savings->DisplayBalance();
?>

28
exam199/index.php Normal file
View File

@@ -0,0 +1,28 @@
<?php
class DatabaseConnect{
public function __construct($db_host, $db_user, $db_pass)
{
echo 'Attempting connection...';
if(!$this->Connect($db_host,$db_user,$db_pass)){
echo 'Connection failed';
} else {
echo 'Connected to '. $db_host;
}
}
public function Connect($db_host, $db_user, $db_pass){
if(!mysqli_connect($db_host,$db_user,$db_pass)){
return false;
} else {
return true ;
}
}
}
$conn = new DatabaseConnect('localhost','example_user','Kolosnjaj4321!');
?>