User Tools

Site Tools


2019-07-09
LDAP: couldn't connect to LDAP server

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
2019-07-09 [2019/06/26 09:52]
nmckillop created
2019-07-09 [2019/07/21 18:24] (current)
ed
Line 1: Line 1:
 ====== Meeting Notes: Tue 9th July 2019 ====== ====== Meeting Notes: Tue 9th July 2019 ======
  
-Welcome! ​ This is the notes page for our third learn-to-code-from-scratch meeting in The Avalon Bar.  The plan is to run something like this every two weeks to get people up and running with basic web development and system admin.+Welcome! ​ This is the notes page for our third learn-to-code-from-scratch meeting in McPhabbs (downstairs).  The plan is to run something like this every two weeks to get people up and running with basic web development and system admin. 
 + 
 +==== Scratch Notes ==== 
 +Adding this to the top of your programs will force errors to be displayed (as opposed to hiding them from general users). ​ It might help find the source of your problem if you get an 'Error 500'​. 
 +<​code>​ 
 +error_reporting(E_ALL);​ 
 +ini_set("​display_errors",​ 1); 
 +</​code>​
  
 ==== Learn to Code: What we'll do ==== ==== Learn to Code: What we'll do ====
Line 15: Line 22:
  
   - [[Setting up EC2|Setting up an Amazon EC2 instance]] - once that is complete you now have your own server, and you can securely connect to it using Putty (or directly via Linux/​macOS)   - [[Setting up EC2|Setting up an Amazon EC2 instance]] - once that is complete you now have your own server, and you can securely connect to it using Putty (or directly via Linux/​macOS)
-  - [[2019-03-17|Setting up your server with Apache and MySQL]] - the server doesn'​t have much software installed, just a basic terminal, these steps install Apache (the webserver) and MySQL (the database). ​ At this step we also downloaded Adminer (a GUI to manage the database, similar to PHPMyAdmin). +  - [[2019-03-17|Setting up your server with Apache and MySQL]] - the server doesn'​t have much software installed, just a basic terminal, these steps install Apache (the webserver) and MySQL (the database). ​ At this step we also downloaded Adminer (a GUI to manage the database, similar to PHPMyAdmin). Then we went through the Amazon AWS interface, and set up Security Groups for the webserver (HTTP) - this is basically Amazon'​s equivalent of a firewall, we had to open a port to allow access to our IP in a web browser
-  - Then we went through the Amazon AWS interface, and set up Security Groups for the webserver (HTTP) - this is basically Amazon'​s equivalent of a firewall, we had to open a port to allow access to our IP in a web browser+
   - [[Hello World|Writing your first hello world program]] - Now we started with our very first simple program.   - [[Hello World|Writing your first hello world program]] - Now we started with our very first simple program.
   - [[Editing files|Editing files remotely on the server]] - Then we looked at ways to connect directly to the server and edit files using Notepad++ (or by mounting a remote directory in Linux/​macOS)   - [[Editing files|Editing files remotely on the server]] - Then we looked at ways to connect directly to the server and edit files using Notepad++ (or by mounting a remote directory in Linux/​macOS)
   - [[Basic Form|An example of a basic HTML form]] - We created a basic HTML form and showed how to access it's elements in PHP once it had been submitted   - [[Basic Form|An example of a basic HTML form]] - We created a basic HTML form and showed how to access it's elements in PHP once it had been submitted
   - [[2019-04-06|Some example code in PHP to connect to your MySQL server]] - Some others went ahead and got the PHP code working that connects to a database and pulls data out, though we stopped there as we hadn't actually put data into the database yet - maybe next time!   - [[2019-04-06|Some example code in PHP to connect to your MySQL server]] - Some others went ahead and got the PHP code working that connects to a database and pulls data out, though we stopped there as we hadn't actually put data into the database yet - maybe next time!
 +
 +==== Basic Quiz ====
 +
 +This uses elements from the [[Basic Form|basic form]] that we've already seen.  We'll start just using an array of questions, then move to populating an array from a database.
 +
 +First, the questions. ​ Create a file called ''​questions.php''​ and enter something like this:
 +
 +<​code>​
 +
 +<?php
 +
 +$questions[0]['​question'​] = "What was originally considered to be the ninth planet from the Sun but after 1992 had its status as a large planet changed?";​
 +$questions[0]['​answer'​] = "​Pluto";​
 +
 +$questions[1]['​question'​] = "How many days are there in February in a leap year?";​
 +$questions[1]['​answer'​] = "​29";​
 +
 +$questions[2]['​question'​] = "Give the four initials of the organisation responsible for maintaining a database of drivers in Great Britain?";​
 +$questions[2]['​answer'​] = "​DVLA";​
 +
 +$questions[3]['​question'​] = "Which dance move, in which the dancer moves backwards, was made popular by Michael Jackson?";​
 +$questions[3]['​answer'​] = "​Moonwalk";​
 +
 +?>
 +
 +
 +</​code>​
 +
 +Now, a basic page to choose a question at random, and wait for the answer, let's call it ''​quiz.php'':​
 +
 +<​code>​
 +
 +<?php
 +
 +require("​questions.php"​);​
 +
 +$size_of_question_list = sizeof($questions);​
 +
 +// check if we are in the middle of answering a question (if we are, then
 +// the random_number will already be set), otherwise, choose a number
 +
 +if(empty($_REQUEST['​random_number'​])) {
 +   ​$random_number = rand(0, $size_of_question_list); ​ // there is a bug here
 +} else {
 +   ​$random_number = $_REQUEST['​random_number'​];​
 +}
 +
 +
 +?>
 +
 +<​h1>​Quiz</​h1>​
 +
 +<?php
 +
 +if(!empty($_REQUEST['​user_answer'​])) {
 +   ​$user_answer = $_REQUEST['​user_answer'​];​
 +   ​$question_id = $_REQUEST['​random_number'​];​
 +   echo "<​p>​You answered '​$user_answer'​ for Question #​$question_id</​p>";​
 +
 +   ​if($user_answer == $questions[$question_id]['​answer'​]) {
 +      echo "​Correct :)";
 +   } else {
 +      echo "Wrong :(";
 +   }
 +
 +}
 +
 +?>
 +
 +<​p><​strong>​Question:​ </​strong><?​= $questions[$random_number]['​question'​];​ ?></​p>​
 +<form method='​post'​ action='​quiz.php'>​
 +<input type='​hidden'​ name='​random_number'​ value='<?​= $random_number;​ ?>' />
 +<input type='​text'​ name='​user_answer'​ />
 +<input type='​submit'​ name='​answer_button'​ value='​Answer'/>​
 +</​form>​
 +
 +<​p><​a href='​quiz.php'>​Another random question</​a></​p>​
 +
 +
 +</​code>​
 +
 +==== Getting the questions from a database ====
 +
 +First, lets design a very simple database that can hold both the questions and answers. ​ We'd normally have a table for questions and a table for answers (especially if some questions had multiple answers) but for now we'll stick to one table.
 +
 +We need:
 +
 +    - A question ID (so we can match questions to answers) this'​ll be a number so we can use numbered arrays
 +    - A question (a text field to record the question in) lets say 2000 characters long at most
 +    - An answer (so we know if the user got it right) lets say 500 characters long at most
 +    - A time/date of when the question was last asked
 +
 +Why the time and date?  This'​ll let the '​random experience'​ be a bit less repetitive. ​ More on that later.
 +
 +You can create this manually using the mysql client, or in Adminer (instructions on installing it are [[2019-03-17|here]]).
 +
 +A quick way to create the table in Adminer is to copy and paste this SQL into the **SQL Command** section:
 +<​code>​
 +CREATE TABLE `quiz_questions` (
 +  `question_id` int NOT NULL AUTO_INCREMENT PRIMARY KEY,
 +  `question` varchar(2000) NOT NULL,
 +  `answer` varchar(500) NOT NULL,
 +  `last_asked` datetime NOT NULL
 +);
 +</​code>​
 +
 +That'​ll create the table for you.
 +
 +And now, this SQL will add some test questions to it (or you can enter some yourself by clicking on the newly created "​quiz_questions"​ table and clicking 'New Item'​):​
 +
 +<​code>​
 +INSERT INTO `quiz_questions` (`question`,​ `answer`, `last_asked`)
 +VALUES ('What was originally considered to be the ninth planet from the Sun but after 1992 had its status as a large planet changed?',​ '​Pluto',​ now());
 +INSERT INTO `quiz_questions` (`question`,​ `answer`, `last_asked`)
 +VALUES ('How many days are there in February in a leap year?',​ '​29',​ now());
 +INSERT INTO `quiz_questions` (`question`,​ `answer`, `last_asked`)
 +VALUES ('Give the four initials of the organisation responsible for maintaining a database of drivers in Great Britain?',​ '​DVLA',​ now());
 +INSERT INTO `quiz_questions` (`question`,​ `answer`, `last_asked`)
 +VALUES ('​Which dance move, in which the dancer moves backwards, was made popular by Michael Jackson?',​ '​Moonwalk',​ now());
 +</​code>​
 +
 +
 +Let's create a new file, this one called ''​questions_from_db.php''​ - we'll be using some code from the [[2019-04-06|connecting to your DB sample]]:
 +
 +<​code>​
 +<?​php ​
 +
 +$host = "​localhost";​
 +$username = "​coding_user";​
 +$password = "​cheese";​
 +$db_name = "​coding"; ​
 +
 +$mysql_string = "​mysql:​host=$host;​dbname=$db_name";​
 +
 +$db_connection = new PDO($mysql_string,​ $username, $password);
 +
 +$sql = "​select * from quiz_questions";​
 +$rs = $db_connection->​query($sql);​
 +$rows = $rs->​fetchAll(PDO::​FETCH_ASSOC);​
 +
 +foreach($rows as $row) {
 +  $questions[$row['​question_id'​]]['​question'​] = $row['​question'​];​
 +  $questions[$row['​question_id'​]]['​answer'​] = $row['​answer'​];​
 +
 +}
 +
 +?>
 +</​code>​
 +
 +==== Putting it all together ====
 +Now, if everything has went to plan, you can replace the:
 +<​code>​require("​questions.php"​)</​code>​
 +
 +line from your ''​quiz.php''​ file with:
 +<​code>​require("​questions_from_db.php"​)</​code>​
 +
 +and now, when you enter new questions to the database, they'​ll be available in your quiz.
 +
 +==== We finished? ====
 +
 +This might be too much for one session, but the great thing about the web is how easy it is for us to connect with each other.
 +
 +You can make your questions, in your database, available to anyone else's quiz program with this bit of code, call it ''​export.php'':​
 +
 +<​code>​
 +<?php
 +header("​Content-Type:​ application/​json"​);​
 +require("​questions_from_db.php"​);​
 +echo json_encode($questions);​
 +?>
 +</​code>​
 +
 +Now, you can share your quiz URL with others. ​ The full URL will be http://​YOUR_IP_ADDRESS/​export.php
 +
 +So, back to the ''​quiz.php''​ file (actually anyone else's ''​quiz.php''​) instead of the line that reads
 +<​code>​require("​questions.php"​)</​code>​
 +
 +This code will connect to a fellow coder'​s IP address, download their quiz questions and format them in a way your program can understand them:
 +<​code>​
 +$json_data = file_get_contents("​http://​YOUR_IP_ADDRESS/​export.php"​);​
 +$questions = json_decode($json_data,​ true);
 +</​code>​
 +
 +
 +==== Attendees ====
 +
 +  - Abdullah Khan
 +  - Aidan Rooney
 +  - Aileen McKenzie
 +  - Alan Kennedy
 +  - Allan Mullen
 +  - Angel
 +  - [[https://​glasgow.social/​@Ashmanq|Ashir]] (Organiser)
 +  - Ben Davidson
 +  - Ben Ross
 +  - Benami Ark
 +  - Callum Tennant
 +  - Cesar Del Villar
 +  - Connor Bell
 +  - [[https://​glasgow.social/​@dric|Cédric Chantreau]]
 +  - [[https://​glasgow.social/​@Legomancer|Ed]]
 +  - Erika Anderson
 +  - [[https://​glasgow.social/​@garethk|Gareth King]]
 +  - [[https://​glasgow.social/​@Hammy|Hammy]]
 +  - Jack Hood
 +  - [[https://​glasgow.social/​@John|John Carlin]]
 +  - John Mason
 +  - Karola Jehodek
 +  - Kathryn M
 +  - [[https://​glasgow.social/​@kevin_w|Kevin]]
 +  - MaryF
 +  - meenakshi
 +  - Mike Nelson
 +  - Mohamed Ali
 +  - Nasir Hussain
 +  - [[https://​glasgow.social/​@neil|Neil McKillop]] (Event Organiser)
 +  - Neil Wylie
 +  - Pavlo
 +  - Rameez
 +  - [[https://​glasgow.social/​@rasikapurohit|Rasika Purohit]]
 +  - Stephen Taylor
 +  - [[https://​glasgow.social/​@M|Steven]]
 +  - Victoria B
 +  - [[https://​glasgow.social/​@Star_Anise|Yasmin]]
 +
 +
  
  
2019-07-09.1561539148.txt.gz · Last modified: 2019/06/26 09:52 by nmckillop