-
-
Save RadoRado/9a8381d0389869cb9dfa to your computer and use it in GitHub Desktop.
Примерите от лекцията за предаване на данни между клиента и сървъра / PHP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function v_array_key_exists($keys, $func, $search) { | |
foreach ($keys as $key) { | |
if(!$func($key, $search)) { | |
return false; | |
} | |
} | |
return true; | |
} | |
$keys = array("operation", "numberA", "numberB"); | |
if(!v_array_key_exists($keys, "array_key_exists", $_POST)) { | |
die("Not enough keys"); | |
} | |
function add($a, $b) { | |
return $a + $b; | |
} | |
function sub($a, $b) { | |
return $a - $b; | |
} | |
function mult($a, $b) { | |
return $a * $b; | |
} | |
function divide($a, $b) { | |
return $a / $b; | |
} | |
$operation = $_POST["operation"]; | |
$functionWhitelist = array("add", "sub", "mult", "divide"); | |
if(!in_array($operation, $functionWhitelist)) { | |
die("No such function."); | |
} | |
$result = $operation($_POST["numberA"], $_POST["numberB"]); | |
// key1=value1&key2=value2&... | |
header("Location: index.php?result=" . $result) ; | |
// function navigate($url, $getParameters = array()) { | |
// } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
header('Content-Type: text/html; charset=utf-8'); | |
?> | |
<html> | |
<head> | |
<title>Calculator</title> | |
</head> | |
<body> | |
<form method="post" action="calculator.php"> | |
<label for="numberA">A:</label> | |
<input id="numberA" name="numberA" type="number" /> | |
<br /> | |
<label for="numberB">B:</label> | |
<input id="numberB" name="numberB" type="number" /> | |
<br /> | |
<select name="operation"> | |
<option value="add">Събираме двете числа отгоре</option> | |
<option value="sub">-</option> | |
<option value="mult">*</option> | |
<option value="divide">/</option> | |
<option value="in_array">omg</option> | |
</select> | |
<input type="submit" value="Calculate" /> | |
</form> | |
<h2>Result is:</h2> | |
<?php | |
if(array_key_exists("result", $_GET)) { | |
echo "<p>" . $_GET["result"] . "</p>"; | |
} | |
?> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
session_start(); | |
$count = 1; | |
if (isset($_SESSION["count"])) { | |
$_SESSION["count"]++; | |
$count = $_SESSION["count"]; | |
} else { | |
$_SESSION["count"] = 1; | |
} | |
echo "You are visiting this page for " . $count . " time."; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
session_start(); | |
var_dump($_SESSION); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment