Difference between revisions of "Database Programmering"
From Teknologisk videncenter
(Created page with "==Forberedelse til Mandag== Hav XAMPP klar på jeres computer. XAMPP kan downloades her https://www.apachefriends.org/index.html") |
|||
(9 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
− | ==Forberedelse til | + | ==Forberedelse til Første Kursusdag== |
Hav XAMPP klar på jeres computer. XAMPP kan downloades her https://www.apachefriends.org/index.html | Hav XAMPP klar på jeres computer. XAMPP kan downloades her https://www.apachefriends.org/index.html | ||
+ | |||
+ | ==Link til Classroom== | ||
+ | https://classroom.github.com/a/87PUc6pC | ||
+ | |||
+ | ==Opgave== | ||
+ | Bruger Database (navn og password)<br> | ||
+ | En side til oprettelse af brugere<br> | ||
+ | En side til at verificere et login<br> | ||
+ | Et backup system i json<br> | ||
+ | Lav en profil side, der viser data om den nuværende bruger<br> | ||
+ | Lav et status system (One to Many)<br> | ||
+ | Lav upload af profil billede (Avanceret)<br> | ||
+ | Lav enten et event system, eller et friend system (Avanceret)<br> | ||
+ | |||
+ | ==Eksempler== | ||
+ | JSON (Mere info: [http://php.net/manual/en/function.json-encode.php json_encode] og [http://php.net/manual/en/function.json-decode.php json_decode]) | ||
+ | <source lang="php"> | ||
+ | <?php | ||
+ | class MyClass { | ||
+ | public $name = "Mads"; | ||
+ | public $age = 27; | ||
+ | public $awesome = TRUE; | ||
+ | } | ||
+ | |||
+ | $mads = new MyClass(); | ||
+ | $json = json_encode($mads); | ||
+ | //{"name":"Mads","age":27,"awesome":true} | ||
+ | |||
+ | $newMads = json_decode($json); | ||
+ | echo $newMads->$name; | ||
+ | ?> | ||
+ | </source> | ||
+ | |||
+ | Saving a File (simple, Mere Info: [http://php.net/manual/en/function.file-get-contents.php file_get_contents] og [http://php.net/manual/en/function.file-put-contents.php file_put_contents]) | ||
+ | <source lang="php"> | ||
+ | <?php | ||
+ | file_put_contents("myFile.txt", "PHP is used for Server-Side Applications"); | ||
+ | echo file_get_contents("myFile.txt"); | ||
+ | ?> | ||
+ | </source> | ||
+ | |||
+ | Saving a File (full, Mere Info: [http://php.net/manual/en/function.fopen.php fopen], [http://php.net/manual/en/function.fwrite.php fwrite] og [http://php.net/manual/en/function.fread.php fread]) | ||
+ | <source lang="php"> | ||
+ | <?php | ||
+ | $filename = "myFile.txt"; | ||
+ | $handle = fopen($filename, 'w'); | ||
+ | fwrite($handle, "Now we have far more control!"); | ||
+ | fclose($handle); | ||
+ | $handle = fopen($filename, 'r'); | ||
+ | echo "File Says:\n"; | ||
+ | echo fread($handle, filesize($filename)); | ||
+ | fclose($handle); | ||
+ | ?> | ||
+ | </source> | ||
+ | |||
+ | Password Hashing (Mere Info: [http://php.net/manual/en/function.password-hash.php password_hash] og [http://php.net/manual/en/function.password-verify.php password_verify]) | ||
+ | <source lang="php"> | ||
+ | <?php | ||
+ | $st = "madsbock"; | ||
+ | echo $st . "<br>"; | ||
+ | $hash = password_hash($st, PASSWORD_DEFAULT); | ||
+ | echo $hash . "<br>"; | ||
+ | if(password_verify($st, $hash)) { | ||
+ | echo "Access Granted"; | ||
+ | } else { | ||
+ | echo "Access Denied"; | ||
+ | } | ||
+ | ?> | ||
+ | </source> | ||
+ | |||
+ | Session Variables (Mere Info: [https://www.w3schools.com/php/php_sessions.asp Sessions]) | ||
+ | <source lang="php"> | ||
+ | <!--Script1.php, Setting a session variable --> | ||
+ | <?php | ||
+ | session_start(); //Must be in every script that uses session variables | ||
+ | |||
+ | $_SESSION["abc"] = "dfg"; | ||
+ | ?> | ||
+ | |||
+ | <!--Script2.php, Using a session variable --> | ||
+ | <?php | ||
+ | session_start(); | ||
+ | |||
+ | echo $_SESSION["abc"]; //outputs "dfg" | ||
+ | ?> | ||
+ | |||
+ | <!--Script3.php, Stopping a session --> | ||
+ | <?php | ||
+ | session_start(); | ||
+ | |||
+ | session_unset(); | ||
+ | session_destroy(); | ||
+ | |||
+ | echo $_SESSION["abc"]; //This line will now give error | ||
+ | ?> | ||
+ | </source> |
Latest revision as of 11:02, 7 December 2017
Forberedelse til Første Kursusdag
Hav XAMPP klar på jeres computer. XAMPP kan downloades her https://www.apachefriends.org/index.html
Link til Classroom
https://classroom.github.com/a/87PUc6pC
Opgave
Bruger Database (navn og password)
En side til oprettelse af brugere
En side til at verificere et login
Et backup system i json
Lav en profil side, der viser data om den nuværende bruger
Lav et status system (One to Many)
Lav upload af profil billede (Avanceret)
Lav enten et event system, eller et friend system (Avanceret)
Eksempler
JSON (Mere info: json_encode og json_decode)
<?php
class MyClass {
public $name = "Mads";
public $age = 27;
public $awesome = TRUE;
}
$mads = new MyClass();
$json = json_encode($mads);
//{"name":"Mads","age":27,"awesome":true}
$newMads = json_decode($json);
echo $newMads->$name;
?>
Saving a File (simple, Mere Info: file_get_contents og file_put_contents)
<?php
file_put_contents("myFile.txt", "PHP is used for Server-Side Applications");
echo file_get_contents("myFile.txt");
?>
Saving a File (full, Mere Info: fopen, fwrite og fread)
<?php
$filename = "myFile.txt";
$handle = fopen($filename, 'w');
fwrite($handle, "Now we have far more control!");
fclose($handle);
$handle = fopen($filename, 'r');
echo "File Says:\n";
echo fread($handle, filesize($filename));
fclose($handle);
?>
Password Hashing (Mere Info: password_hash og password_verify)
<?php
$st = "madsbock";
echo $st . "<br>";
$hash = password_hash($st, PASSWORD_DEFAULT);
echo $hash . "<br>";
if(password_verify($st, $hash)) {
echo "Access Granted";
} else {
echo "Access Denied";
}
?>
Session Variables (Mere Info: Sessions)
<!--Script1.php, Setting a session variable -->
<?php
session_start(); //Must be in every script that uses session variables
$_SESSION["abc"] = "dfg";
?>
<!--Script2.php, Using a session variable -->
<?php
session_start();
echo $_SESSION["abc"]; //outputs "dfg"
?>
<!--Script3.php, Stopping a session -->
<?php
session_start();
session_unset();
session_destroy();
echo $_SESSION["abc"]; //This line will now give error
?>