Script

Filter
2004-11-10

Limit

Mysql
Begränsa resultatet av en fråga med Limit
LIMIT can be used in two ways: "LIMIT n " and "LIMIT m , n ".

The first way allows you force MySQL to only return the first n rows that match your WHERE clause
(if you have one), whereas the second option allows you to force MySQL to return the first n rows after the first m that match.

Ex:
"select * from table LIMIT 200, 10"

(Hämtar 10 poster fr o m post 200. Post 201 - 210)

Some databases, most notably Microsoft SQL Server, use the syntax "SELECT TOP 5 * FROM table;"
as opposed to "SELECT * FROM table LIMIT 5;".
2004-11-09

Servervalidering

Php
Några råd och exempel på validering på servern
<html>
<head>
<title>Validera</title>
<body>
<?
/*
* If you are not using magic quotes, always use addslashes() when working with user input destined for databases.
In addition, always use addslashes() whenever your user input will be inside quotes.

* Consider using strip_tags() to make sure people cannot insert rogue HTML into your pages.

* Never include() a file using a variable unless you are certain the variable cannot come externally.
While "include($var);" might look nice on the surface, it does not take much effort for your users to
set $var to be a sensitive file on your system.

* Always remember that your users might submit no value at all, in which case you need to check for
a variable's existence before you check its value.

* Don't assume that client-side validation is enough - users can easily disable scripting on their
machine, or find other ways around your client-side verification

* Remember that users can enter "Elephant" for their age - don't assume that users entered anything like what you asked them to.

* Variable variables and variable functions that rely on user input should be viewed with extreme caution:
don't give your users any such easy chances to damage your system with bad input.
*/


if (!isset($_POST['Form']['Languages'])) {
    $_POST['Form']['Languages'] = "No languages";
} else {
    $_POST['Form']['Languages'] = implode(', ', $_POST['Form']['Languages']);
}

if (!isset($_POST['Form']['Story'])) {
    $_POST['Form']['Story'] = "No story";
} else {
    $_POST['Form']['Story'] = str_replace("\n", "<BR />", $_POST['Form']['Story']);
}

if (isset($_POST['Form'])) {
    import_request_variables("p", "z");
    $missingfields = array();
    $required = array("FName"=>"First Name", "LName"=>"Last Name", "Age"=>"Age");
        
    while (list($var, $val) = each($required)) {
        if (isset($zForm[$var]) && $zForm[$var] != '') {
            // checking value further here:
            echo "<b>", $zForm[$var], "</b><br />\n";
            echo 'is_string: ', (int)is_string($zForm[$var]), "<br />\n";
            echo 'is_numeric: ', (int)is_numeric($zForm[$var]), "<br />\n";
            echo 'is_array: ', (int)is_array($zForm[$var]), "<br />\n";
            echo 'is_object: ', (int)is_object($zForm[$var]), "<br />\n<br />\n";
            
            echo 'ctype_alnum: ', (int)ctype_alnum ($zForm[$var]), "<br />\n"; //  matches A-Z, a-z, 0-9
            echo 'ctype_alpha: ', (int)ctype_alnum ($zForm[$var]), "<br />\n"; //  matches A-Z, a-z
            echo 'ctype_cntrl: ', (int)ctyp
      e_cntrl ($zForm[$var]), "<br />\n"; //  matches ASCII control characters
            echo 'ctype_digit: ', (int)ctype_digit ($zForm[$var]), "<br />\n"; //  matches 0-9
            echo 'ctype_graph: ', (int)ctype_graph ($zForm[$var]), "<br />\n"; //  matches values that can be represented graphically
            echo 'ctype_lower: ', (int)ctype_lower ($zForm[$var]), "<br />\n"; //  matches a-z
            echo 'ctype_print: ', (int)ctype_print ($zForm[$var]), "<br />\n"; //  matches visible characters (not whitespace)
            echo 'ctype_punct: ', (int)ctype_punct ($zForm[$var]), "<br />\n"; //  matches all non-alphanumeric characters (not whitespace)
            echo 'ctype_space: ', (int)ctype_space ($zForm[$var]), "<br />\n"; //  matches whitespace (space, tab, new line, etc)
            echo 'ctype_upper: ', (int)ctype_upper ($zForm[$var]), "<br />\n"; //  matches A-Z
            echo 'ctype_xdigit: ', (int)ctype_xdigit ($zForm[$var]), "<br /><br />\n"; //  matches digits in hexadecimal format
            
        } else {
            $missingfields[$var] = $val;
        }
    }

    if (count($missingfields)) {
        print "You missed out one or more fields:<BR />";

        while(list($var, $val) = each($missingfields)) {
            print $val . "<BR />";
        }
        echo '<br /><form><input value="Back" type="button" onClick="history.back();"></form>';
        exit;
    } else {
        print "<BR />Form passed!<BR />";
        
        foreach ($_POST['Form'] as $nyckel => $varde) {
            echo '<li>' . $nyckel . ' = ' . $varde;
        }

        exit;
    }
}

?>
<br />
<br />

<form method="post" action="validate.php">
First Name<br />
<input type="text" name="Form[FName]" /> *<br />
Last Name<br />
<input type="text" name="Form[LName]" /> *<br />
Age<br />
<input type="text" name="Form[Age]" /><br />
Story<br />
<textarea name="Form[Story]" rows="8" cols="40"></textarea><br />
<br />
Languages known:<br />
<input type="checkbox" name="Form[Languages][]" value="PHP" /> PHP<br />
<input type="checkbox" name="Form[Languages][]" value="CPP" /> C++<br />
<input type="checkbox" name="Form[Languages][]" value="Delphi" /> Delphi<br />
<input type="checkbox" name="Form[Languages][]" value="Java" /> Java<br />
<input type="submit" />
</form>

</body>
</html> 
2004-11-08

Inställningar i php.ini

Php
Ta reda på viktiga inställningar i php.ini
<? 
echo 'get_magic_quotes_gpc = ' , get_magic_quotes_gpc() , "<br>\n";
echo 'display_errors = ' , ini_get('display_errors') , "<br>\n";
echo 'register_globals = ' , (int)ini_get('register_globals') , "<br>\n";
echo 'post_max_size = ' , ini_get('post_max_size');
?> 
2004-10-31

Default ny sida

Html
Mall för att starta en ny html-sida
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<meta http-equiv="imagetoolbar" content="no" />
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<meta name="description" content="" />
<meta name="keywords" content="" />
<meta name="author" content="" />
<meta name="generator" content="WebMan - http://www.rosiro.se" />
<meta name="robots" content="noarchive" />
<meta name="Copyright" content="&copy; 2008 Rosiro" />
<script type="text/javascript" src=""></script>
<link rel="stylesheet" href="" type="text/css" />
<style type="text/css"><!--
body {
    margin: 0;
    padding: 0;
    background-color: #fff;
    font: 80%/120% Verdana, Geneva, Arial, Helvetica, sans-serif;
    color: #333;
}
a { text-decoration: none; }
a:active { color: #f00; }
a:link { color: #333; }
a:visited { color: #666; }
a:hover { color: #06f; }
--></style>
</head>
<body>
<table border="1" cellpadding="0" cellspacing="0" align="center">
<tr>
<td></td>
</tr>
</table>
</body>
</html> 
2004-10-10

Thispage

Php
Tar reda på vad sidan man är på heter
$thispage = substr($_SERVER['PHP_SELF'], strrpos($_SERVER['PHP_SELF'], "/") + 1);

// Smartare, alt 2:
$thispage = basename($_SERVER['PHP_SELF']);

// Utan extension:
$thispage = basename($_SERVER['PHP_SELF'], '.php'); 
🙂