Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@RadoRado
Created March 27, 2013 21:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save RadoRado/9a8381d0389869cb9dfa to your computer and use it in GitHub Desktop.
Save RadoRado/9a8381d0389869cb9dfa to your computer and use it in GitHub Desktop.
Примерите от лекцията за предаване на данни между клиента и сървъра / PHP
<?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()) {
// }
<?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>
<?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.";
<?php
session_start();
var_dump($_SESSION);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment