Results 1 to 8 of 8
Hybrid View
-
20th March 2013 12:38 #1
test.php
$error . , $error .PHP Code:
<?php // Block 1
if($_SERVER['REQUEST_METHOD'] == 'POST'){
$name = $_POST['name'];
$pass = $_POST['pass'];
$error = '';
if(empty($name)){
$error .= 'Enter a name<br>';
}
if(empty($pass)){
$error = 'Enter a password<br>';
}
if(strlen($name) && strlen($pass)){
$error .= '';
}
if($error){
header('Location: test.php?status=error');
exit;
}else{
header('Location: test.php?status=success');
exit;
}
}
?>
<?php // Block 2
if(isset($_GET['status'])){
if($_GET['status'] == 'error'){
echo 'Error';
}elseif($_GET['status'] == 'success'){
echo 'Thank you';
}
}
?>
<form action="#" method="post">
Name: <input type="text" name="name" id="name"><br>
Pass: <input type="password" name="pass" id="pass"><br>
<input type="submit" value="Send">
</form>
. .
2 1 , $error .
.
-
20th March 2013 13:52 #2Mire-x
Join Date: Apr:2005
Location: Sofia
Posts: 763
, , - "header()" "echo []". , ( ) , GET ( "?status=error" / "?status=success" "?status=0" / "?status=1", , 0 "Thank you.", 1 "Enter a name." ..). :
PHP Code:<?php
// Status codes
define('STATUS_OK', 0);
define('STATUS_EMPTY_NAME', 1);
define('STATUS_EMPTY_PASSWORD', 2);
// Status messages
$statusMessages = array(
0 => 'Thank you.',
1 => 'Enter a name.',
2 => 'Enter a password.'
);
// Sends a 'Location' header and exits
function redirect($statusCode)
{
header('Location: ' . $_SERVER['PHP_SELF'] . '?status=' . strval($statusCode));
exit;
}
// POST (name, password)
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$name = $_POST['name'];
$pass = $_POST['pass'];
if (empty($name)) {
redirect(STATUS_EMPTY_NAME);
} elseif (empty($pass)) {
redirect(STATUS_EMPTY_PASSWORD);
} else {
redirect(STATUS_OK);
}
}
// GET (status)
if (isset($_GET['status'])) {
if (array_key_exists($_GET['status'], $statusMessages)) {
echo $statusMessages[$_GET['status']] . '<br />';
} else {
echo 'Unknown error.';
}
}
?>(10b) || !(10b)
-
20th March 2013 14:26 #3
Tarvin.
- , POST , - , ..
, ( / / GET ) 4- ( ) , , (error, warning, notice ..), ( ). , View- .. , / - race condition ( ).
- Tarvin ( ), , ( ) .
-
20th March 2013 14:31 #4
-
20th March 2013 15:35 #5
Tarvin , .
, , 15 , , test.php?status=1&status=3&status=8&stat us=12 test.php?status=error test.php?status=success .
vbKing
-
20th March 2013 16:30 #6Mire-x
Join Date: Apr:2005
Location: Sofia
Posts: 763
"status=1&status=3&status=8" , , $_GET['status']? 1, 3 8?

, (.. 1 ), , . : 1 = Missing name. 2 = Missing password. 4 = Invalid name. 8 = Invalid password. , "status" . , , , ( 0, , ). , , "status=3" " 1 + 2", "status=12" " 4 + 8".
: "" . , 1, 2, 4, 8..., "a", "b", "c", "d". "status=ab" "status=cd" "status=3" "status=12" . , , . , "?status=", "?status", URL-. empty($_GET['status']) , , .
, , GET , , "?status=..." URL-. , , .
EDIT: , , , .
PHP Code:<?php
// Status codes
define('STATUS_EMPTY_NAME', 1);
define('STATUS_EMPTY_PASSWORD', 2);
define('STATUS_INVALID_NAME', 4);
define('STATUS_INVALID_PASSWORD', 8);
// Status messages
$statusMessages = array(
0 => 'Thank you.',
1 => 'Enter a name.',
2 => 'Enter a password.',
4 => 'Name must be at least 3 characters long.',
8 => 'Password must be at least 8 characters long.'
);
// Sends a 'Location' header and exits
function redirect($statusCode)
{
header('Location: ' . $_SERVER['PHP_SELF'] . '?status=' . strval($statusCode));
exit;
}
// POST (name, password)
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$name = $_POST['name'];
$pass = $_POST['pass'];
$status = 0;
if (empty($name)) {
$status += STATUS_EMPTY_NAME;
} elseif (strlen($name) < 3) {
$status += STATUS_SHORT_NAME;
}
if (empty($pass)) {
$status += STATUS_EMPTY_PASSWORD;
} elseif (strlen($pass) < 8) {
$status += STATUS_SHORT_PASSWORD;
}
redirect($status);
}
// GET (status)
if (isset($_GET['status'])) {
$status = $_GET['status'];
if (is_numeric($status)) {
if ($status == 0) {
echo $statusMessages[0];
} else {
foreach($statusMessages as $value => $message) {
if ($status & $value > 0) {
echo $message . '<br />';
}
}
}
} else {
echo 'Unknown error.';
}
}
?>Last edited by Tarvin; 20th March 2013 at 17:46.
(10b) || !(10b)




Reply With Quote

Lenovo ThinkPad 15 IdeaPad 15
5th May 2023, 22:16 in