Presentation is loading. Please wait.

Presentation is loading. Please wait.

Faculty of Sciences and Social Sciences HOPE User Sessions & The Include Statement Stewart Blakeway FML 213

Similar presentations


Presentation on theme: "Faculty of Sciences and Social Sciences HOPE User Sessions & The Include Statement Stewart Blakeway FML 213"— Presentation transcript:

1 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE User Sessions & The Include Statement Stewart Blakeway FML 213 blakews@hope.ac.uk

2 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Last Week myPhpAdmin – Created a database – Tables – Fields Inserted Data

3 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Recap 1.Create a connection to the SQL Server $conn = mysql_connect (“localhost”, “root”, “root”); 2.Select the database mysql_select_db (“database”, $conn); 3.Construct the SQL statement $sql = (“what I want to do with the database”); 4.Execute the SQL mysql_query ($sql,$conn);

4 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE To insert data $sql = (“INSERT INTO table VALUES (‘value1’, ‘value2’, ‘value3’,… ))”; or $sql = (“INSERT INTO table (fieldname1, fieldname2,fieldname3,…) VALUES ( ‘value1’, ‘value2’, ‘value3’,… ))”;

5 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE To get data $sql = (“SELECT * FROM table”); or $sql = (“SELECT * FROM table WHERE fieldname = ‘value’”);

6 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Variations (Keywords) DistinctOrNot Null WhereTopUnique AndWildcardsPrimary Key Order ByAliasForeign Key UpdateJoinCheck DeleteInner JoinDefault LikeLeft JoinCreate Index InRight JoinConstraints BetweenFull JoinUnion

7 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE What will we cover today The include statement Getting Data User Sessions

8 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Why To save coding! If you wish to change the design of the corporate logo, motto, navigation bar for example, it will save changing all your pages You need to authenticate the user before allowing them to add records to your database

9 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE The Include Statement The include statement will include code into your existing document This is an efficient way of scripting and maintains consistency Why not just make a template? Because it is as easy to use include!

10 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Example – head.php home | about | courses | tutors | contact us register | log in

11 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE A file called Register.php Forename Surname Email Address Password Confirm Password

12 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Order of Precedence 1.Get the form working! its much easier to work with if the code is kept as simple as possible, formatting code for images, buttons, hyperlinks etc will only add code – adding more work decoding 2.Apply the templates to make it look pretty once all the hard coding – i.e. connection strings, sql statements, passing of $POST variables are done you can then make it look pretty!

13 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE ONE LINE! One line of code is all it takes include (“myfile.php”);

14 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Recap You website will probably consist of 10 – 15 pages (possibly many more) If you change the design of the header, footer, navigation bar on one page you should change it on the rest! Consistency. Using include ensures that only one page needs changing, the rest will update automatically include (“filename”);

15 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE User Sessions You will have to authenticate the log in of the user in order to allow the addition of records into the database You have to follow certain steps in order to ensure that the user is who they claim to be

16 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Authentication 1.Display a login form 2.Get the user details 3.Match the user details against authorised users that are stored in the database 4.Remember that the user is authenticated when they move from one page to the next – only if the details match What would you do if the details did not match?

17 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Not authorised? 1.Display a suitable message – username or password incorrect. and 2.Give the user another chance to login, they could of pistyped – maybe at this point give them a hint or 2.Redirect the user to a Register page

18 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE The Login Page start a session if page not viewed { display the form to accept input } else { 1. get the details from the form 2. create an SQL statement that will match the details obtained from the form against the database 3. if details match, update the session to reflect this }

19 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Starting a session <?php session_start(); ?> starting a session MUST be the first thing you do

20 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Checking You can check that the session has started by outputting the session id echo session_id();

21 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Super Global Variables A variable can be set inside a session $_SESSION[‘variableName’] = “ hello ” ; Like $_POSTthe namethe value

22 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Stopping Sessions session_stop();

23 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE The SQL $user = $_POST[‘username’]; $pw = $_POST[‘password’]; $sql = "SELECT * FROM user WHERE username = '$user' AND password = '$pw'"; What does * mean ?What is user?Where is username?Where is this from?

24 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Execute the SQL $result = mysql_query ($sql,$conn); Put the data from the database in here.

25 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE The Check $record = mysql_num_rows($result); if ($record == 0) { echo "Incorrect Username or Password"; } else { echo "LOGIN OK"; $_SESSION['authorised']='yes'; $_SESSION['user']=$user; echo session_id(); }

26 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE What have we done? 1.Started a session 2.Obtained user details from the login form 3.Matched them against authorised users in the database 4.Created a global variable called authorised and assigned the value yes 5.Created a global variable called user and assigned the value username.

27 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Dynamic Web Pages Users should see appropriate information – Should be able to view general information if not logged in – Student (if logged in) should be able to view resources Lectures, Workshop Exercises, etc – Tutor (if logged in) should be able to add resources Lectures, Workshop Exercises, Quizzes, New Students, etc – Administrator should be able to do anything Authorise new tutors, delete tutors, add courses, etc The fact that we started a session makes this very easy

28 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE User trying to view course? if user not logged in { display login link display register link } else { display course }

29 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Checking if the user has logged in <?php if (!isset($_SESSION['authorised'])) { echo ("not authorised“); echo (" Login | Register “); } else { // display course } ?>

30 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Functions if (!isset($_SESSION['authorised'])) { notAuthorised(); } else { displayCourse(); }

31 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Summary Include Statement Sessions – starting – declaring variables – assignment to variables – retrieving variables

32 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE SQL QUIZ Q1 Which is the correct to syntax to obtain all records from tblPerson? a)$result = mysql_connect ( “ tblPerson ”, “ * ”,$conn); b)$result = mysql_query ( “ SELECT * FROM tblPerson ”,$conn); c)$result = mysql_select_db ( “ * ” FROM tblPerson,$conn); d)$result = mysql ( “ SELECT all FROM tblPerson ”,$conn); tblPerson

33 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE SQL QUIZ Q2 What is the purpose of DISTINCT ? a)To only list unique values in columns b)To only list the first row c)To list the first row only if unique d)To list all the rows and columns tblPerson

34 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE SQL QUIZ Q3 What is the correct syntax to add a new row ? a)$sql = “ INSERT INTO tblPERSON VALUES ( ‘ 4 ’, ‘ HUGHES ’, ‘ JAMIE ’, ‘ SOMEWHERE ’, ‘ LIVERPOOL ’,$conn) ” b)$sql = “ ADD INTO tblPERSON VALUES ( ‘ 4 ’, ‘ HUGHES ’, ‘ JAMIE ’, ‘ SOMEWHERE ’, ‘ LIVERPOOL ’,$conn) ” c)$sql = “ INSERT INTO tblPerson VALUES ( ‘ 4 ’, ‘ HUGHES ’, ‘ JAMIE ’, ‘ SOMEWHERE ’, ‘ LIVERPOOL ’,$conn) ” d)$sql = “ ADD INTO tblPERSON VALUES ( ‘ 4 ’, ‘ HUGHES ’, ‘ JAMIE ’, ‘ SOMEWHERE ’, ‘ LIVERPOOL ’,$conn ” ) tblPerson

35 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE SQL QUIZ Q4 What is the correct syntax to get the column Lastname in ascending order ? a)$sql = “ SELECT LastName FROM tblperson ORDER LastName ASC ” ; b)$sql = “ GET LastName FROM tblperson ORDER LastName ASC ” ; c)$sql = “ SELECT * FROM tblperson ORDER LastName ASC ” ; d)$sql = “ SELECT LastName FROM tblperson ORDER BY LastName ASC ” ; tblPerson

36 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE SQL QUIZ Q5 What is the correct function to get a row from $data returned from the database ? a)mysql_get_line($data); b)mysql_fetch_array($data); c)mysql_obtain_row($data); d)mysql_retrieve_row($data); tblPerson

37 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE SQL QUIZ Q6 usernamepassword BLAKEWAYhahaifidtellyou HARTLEYmypw HUGHESblahblah HUNTERliverpool LEARMONDwolves How many rows and columns are returned? $conn = mysql_connect (“localhost”, “root”, “”); mysql_select_db (“bookShop”); $mysql = (“SELECT * FROM user”); $result = mysql_query($sql,$conn); $conn = mysql_connect (“localhost”, “root”, “”); mysql_select_db (“bookShop”); $mysql = (“SELECT password FROM user”); $result = mysql_query($sql,$conn); $conn = mysql_connect (“localhost”, “root”, “”); mysql_select_db (“bookShop”); $mysql = (“SELECT * FROM user WHERE PASSWORD = ‘liverpool’”); $result = mysql_query($sql,$conn); $conn = mysql_connect (“localhost”, “root”, “”); mysql_select_db (“bookShop”); $mysql = (“SELECT * FROM user WHERE password = ‘liverpool’”); $result = mysql_query($sql,$conn); $conn = mysql_connect (“localhost”, “root”, “”); mysql_select_db (“bookShop”); $mysql = (“SELECT * FROM user WHERE password = ‘liverpool’”); $result = mysql_query($sql,$conn); echo $result; $conn = mysql_connect (“localhost”, “root”, “”); mysql_select_db (“bookShop”); $mysql = (“SELECT * FROM user WHERE password = ‘liverpool’”); $result = mysql_query($sql,$conn); echo $result[username]; $conn = mysql_connect (“localhost”, “root”, “”); mysql_select_db (“bookShop”); $mysql = (“SELECT * FROM user WHERE password = ‘liverpool’”); $result = mysql_query($sql,$conn); $row = mysql_fetch_array($result); echo $row[password]; What will be displayed on screen?

38 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Any Questions?


Download ppt "Faculty of Sciences and Social Sciences HOPE User Sessions & The Include Statement Stewart Blakeway FML 213"

Similar presentations


Ads by Google