Class Checkpoint

Making Parkwood Vale · Part 8 of 13

Part 7b

Make sign in and register work: PHP sessions, checks, hashed passwords and a protected account page.

We connect the forms from Part 7a to the database. Users can register, sign in and reach a protected account page, and passwords are stored hashed. In this part: 0:00 A correction: add your database name to connection.php 4:03 sign-in.php: reading the form data 8:20 Checking for empty fields 12:40 Looking up the user in the database 18:25 Sending a failed sign in back with a message 20:41 Showing the message once with sessions 28:16 Renaming the page to login.php and skipping it when already signed in 31:14 Storing the user's details in the session 35:00 Making account.php a protected page 39:17 signup.php: checking the registration form 46:00 Adding the new user to the database 54:22 Hashing passwords Forgot password is built in Part 8. Software: Adobe Dreamweaver, UwAmp, HeidiSQL, PHP, MySQL.
Transcript
  1. Welcome to part two of this week's session then.
  2. So in part one, if you skipped over it, that's perfectly fine.
  3. What we did was we set up our sign-in form, our register form, and our forgot
  4. password form. In part two, we're going to have a look at the PHP in the back end of
  5. this to see how we check against that database to verify this information.
  6. This is the transactional element of our transactional website.
  7. So let's begin.
  8. Okay, James from the Future here, just two seconds of your time then.
  9. I've had a couple of people come to me with issues, emailing me.
  10. We've been going back and forth trying to figure out what the issue is, and it's
  11. always come back to the same thing.
  12. If you're on video 7B, which this has just been spliced into, then you're probably
  13. working on the section, if I can just get my file section open here, where it is the
  14. reset password element. So password reset, going through this section here, sending
  15. the email, I'm finding that you're having issues, perhaps with a register section as
  16. well, where it's giving you random issues and you're not too sure why.
  17. It seems somewhere within the video series, some people have seen it, some people
  18. haven't seen it, but there's a small change to this connection config file.
  19. So in my connection folder, in my, sorry, config folder, in my connection file,
  20. I have mine set up that looks like this.
  21. It has the database in place and it's got the database name.
  22. That database name comes directly from within my
  23. UwAmp folder. So let me just make sure UwAmp is running a second.
  24. And within that UwAmp section, then when it is up and running, and I can connect to
  25. that database and it will allow me to see what that database is called.
  26. Obviously, this is gonna be unique to each of you.
  27. If you're just plainly copying my database set of my structure and my everything,
  28. then it will still be Parkwood Vale.
  29. If you've adjusted it in any way, it will be whatever your database is.
  30. So I'm just connecting directly into Heidi here.
  31. We've got Parkwood Vale, and in there is the name of my database.
  32. So in this section here, the change that needs to happen
  33. place. Yours may well look like this.
  34. And that is that we have connected to our backend database, but we haven't
  35. told it specifically which one we're looking at.
  36. And if I go back into Heidi here, then you'll see there's an information schema,
  37. there's a MySQL, my park reveal database I want to use, a performance database, a
  38. phpMyAdmin database, and a sys database.
  39. They are all available on that connection.
  40. So when I connect a local host using the username root and password root like I've
  41. done so here that will work fine but it won't know what database to use.
  42. So the change I've been saying to people to make is simply put in db equals and in
  43. speech marks then the name of your database.
  44. In my case Parkwood Vale. Now that's brilliant to set that up there and again put in
  45. the comment saying this is the my
  46. small y SQL database
  47. name which is brilliant in this connection we also need to edit
  48. this here then we need to add in the end here just simply tell it to use the DB we
  49. wanted to use and that way there that should pick up use this DB and slot
  50. it directly in place if you having issues then with 7b at all please
  51. check this connection file please make sure you've got that in place i'm going to
  52. slot this at the start of 7b so hopefully you do this before going further forward
  53. if it's still not working by all means reach out email me and i'll give you some
  54. support on how we can get that working okay thank you back to the video
  55. okay so back in the student desktop then i'm going to start off with our sign in
  56. function So I'm going to go over to my site.
  57. I'm going to create a new file.
  58. I'm going to call this sign in dot PHP and just drop that dot HTML off
  59. the end there. Open that up and delete all of that out.
  60. With that sign in setup, then we're going to tell it that we are working in PHP.
  61. And the very first thing we have to do after the clear our PHP tag is start a
  62. session. Now, a session then is a,
  63. simply put, a session is a cookie stored on the PHP
  64. server. Cookies can store data, they can store information about where you've been,
  65. what IDs you've clicked on, what different elements you've used.
  66. Sessions work in the same way.
  67. A session will have a session ID that every time we go back to it, it knows which
  68. session we're running from.
  69. and that session can be used to store information on our php server
  70. securely so that we can include information such as login information
  71. so i'm going to start my session at the top there then the next line i'm going to do
  72. is i'm going to include so i want to use my server so i'm going to include my
  73. config. So config that's just there forward slash
  74. connection dot PHP.
  75. So that knows then that while running this sign in page, I need to be referring to
  76. this page here.
  77. That's set up then. What I'm going to do is start by looking at how we output
  78. information. So let me just save that a second, push that for a second and open up
  79. my page. As you can see,
  80. place already if i click on submit it comes back and says sign-ins not found let's
  81. see sign in i did that right i didn't push the sign in page there i did that folder
  82. all right if i reload that yes i do want to continue and it comes back with nothing
  83. it's a blank page it hasn't done anything yet what it's done is started a session up
  84. and included my connection and that's it What I can do is I can print
  85. underscore R and from that print R command, I can use my dollar
  86. sign underscore post to access my post array.
  87. And actually, if I just leave it like that, it should print the whole of my post
  88. array.
  89. Now, let's put that up, refresh my page, click yes to continue.
  90. And you can see that I have sent the information in the background of email is cpd
  91. at mrjjones.co.uk.
  92. is blank they come directly from my account page
  93. where I set them up here and I've given them these names here email and password and
  94. just to show that this underscore is underscore
  95. a demo save that let's check that page back
  96. up go back to it and refresh it just so that it knows for my password in and
  97. email let's try that again and make sure we put it up the right page account but
  98. there we go let's put that there refresh this and just a double check and going to
  99. inspect that there this is a demo in the name so that should all work as I expect it
  100. to and if I put my email address in and submit it it now says this is a demo just
  101. like it should have done first time around which would have looked so much more
  102. slick never mind but any name I give it there is the name that I'm referencing as I
  103. go forward. So let's put that back to email, save that, put that,
  104. and jump back into this section here.
  105. I can directly target one element by referencing its location
  106. within my post array. So I've got my print email there.
  107. If I save that and I go back to sign in and put that page up and I go
  108. back, refresh this page to make sure it's updated,
  109. submit it and it just gives me my content.
  110. So we're going to use this element, this way of passing information to check if the
  111. information is empty.
  112. First thing I want to do is I want to set up an array.
  113. It's going to be called errors and it's going to be a simple blank array.
  114. Now I'm going to say that if and I'm going to use a negative on is
  115. set so that negative element means not so if it's not set in this section
  116. here then I'm going to go into the next line I'm going to say dollar sign
  117. errors and I'm going to reference
  118. anywhere within it and just simply say the email
  119. field must be
  120. must not be empty like I say must be complete must not be empty same
  121. sort
  122. I'm going to repeat that process for my password.
  123. So I'm going to say password.
  124. And under here, I'm going to say the password field must not be empty.
  125. So that's going to check to see if my post email and post password have been set.
  126. And if they haven't been set, it's going to give it an error.
  127. So I'm going to say then that if, again, negative empty errors.
  128. errors so I'm checking if errors contains anything if errors does contain
  129. something then I want to redirect my user back but to start with I'm gonna just
  130. simply print underscore errors
  131. print are errors let's do that right let's actually
  132. put it in the correct language.
  133. Save that, put that, and refresh our page and see what happens.
  134. Continue.
  135. It's come up with nothingness for me there so far.
  136. So according to that, then it's quite happy that I've entered everything in place,
  137. but I know for a fact they haven't.
  138. So let's go back and just double check.
  139. Submit and it's not giving me my errors.
  140. So if is set, we should have been able to do this in one
  141. line without having to wrap in braces, but let's check some braces in just to be
  142. safe.
  143. There we go. So if password is not set.
  144. There you go.
  145. And let's just for a second print errors to see if it is working as
  146. expected.
  147. Post that.
  148. Refresh our page. It is giving me an empty array.
  149. okay so if is set post do to do to do
  150. why is that not if empty
  151. let's
  152. try that one
  153. there we go it's set was the wrong command it's not empty so if i remove that for a
  154. second then and try that again post that refresh
  155. my page it says that the email field must not be empty if i go back
  156. and i empty the email address don't save that page i submit that page
  157. if not empty if of course
  158. and post that so with that fix in place what i've done is taking the negative away
  159. saying if it's empty for email if it's empty for password then put it into errors
  160. and if errors isn't empty then print it so now if i refresh this we get
  161. email must not be empty password must not be empty and they are stored.
  162. So we've got some basic detection in place.
  163. What we could do at this stage here then is redirect our user back to our login page
  164. with a message in session telling them that's error but we're gonna go a little bit
  165. step further forward first. I'm gonna say else so
  166. thank you Dreamweaver
  167. otherwise if it is if it's not empty so if my error here doesn't
  168. does contain
  169. then what I want it to do is process it.
  170. This is where our SQL commands come into place.
  171. I'm going to do a very simple select to start with.
  172. So I'm going to select, I want to know the email.
  173. I want to know the, let's double check in Heidi first.
  174. Before I jump too far ahead, let's see what we've called them.
  175. So let's open up Heidi on Parkwood Vale in my users table.
  176. I have called them email for name surname so that's what I want to bring back email
  177. for name
  178. surname from and it's going to be our users table
  179. where and this is where we're going to start pulling in some information
  180. now I'm going to start off first of all saying email equals
  181. and I'm going to put in single quotes.
  182. I'm going to say and password equals and I'm going to put in single
  183. quotes and terminate that command there.
  184. Now there's my SQL section and I want to put in place my
  185. post email and post password in those sections there.
  186. So at that point I need to break out of my PHP command using my speech
  187. marks, use four stops as a joiner and put that in place.
  188. I'm going to do the same thing here.
  189. Break out of it, four stops, and join.
  190. And the password element is going to come here.
  191. Now, I can echo that
  192. $SQL command so I can see exactly what's going to be run on
  193. my server.
  194. I come back here for a second and I go back here and I put in my email address for a
  195. change and I put in password 123
  196. as you can see and submit it. I don't get anything being shown.
  197. Okay so let's see what's happening.
  198. If error is there, no. So SQL, print SQL.
  199. well. Let's go print dollar sign errors.
  200. Ah, same old error, human error, I forgot to
  201. click put.
  202. Refresh and there we go. That's as I'd expect to see it.
  203. It wants to select this information from my database.
  204. Now if I copy that using the copy command into a query here as you can
  205. see I can just paste it and run that query it finds my one user row
  206. for me.
  207. So what I need to do next knowing that works and gives me my right output is check
  208. how many results I've got.
  209. I can use another if command and the if command is going to check to see where my
  210. connect up my query or what number of results are in my query
  211. now I've created my query but I haven't yet run it
  212. so what I need to do is store the answer somewhere I'm going to store in a variable
  213. called result I'm going to say equals connection I'll explain where that comes
  214. from in two seconds time and the is going to query
  215. simply SQL.
  216. And it is $sql because it's my variable and that's going to run there.
  217. Now if I save that for a second I want to explain where this connection comes from.
  218. It comes out of my connection PHP page which if I open that up it's done here my
  219. connection is a new SQLI connection.
  220. So it's using that connection to query my database.
  221. Now in here as I was typing
  222. say is dollar sign results so I'm using a result variable and I'm going to
  223. check a property in there called num underscore rows
  224. and it's going to check to see if the number of
  225. rows is equal to one so
  226. realistically in your database you should only ever have one
  227. match on an email and password
  228. If there is a match in our email and password combination, right now I'm going to
  229. just echo simply, userFact.
  230. If there's not,
  231. I'm going to echo simply, so else, echo,
  232. and I'm going to say, no
  233. userFact.
  234. Now, with all of that in place, let's save it.
  235. Let's not make the same mistake I always make.
  236. place correctly and there's that there.
  237. If I was to refresh this page now, because the information is still on my server, it
  238. should come back with user found because it's found my single user, which is jjohns.
  239. I'm going to my table here though and change the password to password 1234
  240. and then refresh it. It will say no user found because it's going through our logic,
  241. it's performing our query and it's saying whether it's found our user or not.
  242. So there is our basic elements.
  243. Now we have in place three checks.
  244. We have a check to make sure it's been entered correctly, we have a check to see if
  245. there are any errors, and we have our check to see if the user exists.
  246. If the user doesn't exist or if we get any errors here, what we're actually going to
  247. do is redirect them back to that login page.
  248. The way we do that is we set the header information within our PHP and we say that
  249. the location within our header should be and then I can go full
  250. stop backslash account and that's going to take me back to my account page now
  251. we have that session in place and we can use that session for our error
  252. messages so dot sign underscore session and session just like post is
  253. a array and i can put in place here a message to pass in so i'm going to say
  254. sign in error and is going to be equal to
  255. and i'm simply going to say
  256. um well realistically we don't need to tell them which one shouldn't be empty we're
  257. going to say error on the signed in page and just have
  258. that set in place that should if i delete that print now redirect me
  259. straight back to my account page when one of these elements is empty but i won't see
  260. this variable and i'll explain why in a second i'm going to use this same setup here
  261. i'm going copy this across and that is going to go in for my no user found here and
  262. i'm just going to say
  263. it's not anymore it's gonna be instead you have
  264. supplied incorrect
  265. user details now we never actually state
  266. what user details are incorrect just the fact that they are incorrect we wouldn't
  267. want to have a system whereby the user can guess the username and password
  268. combinations by repeatedly over and over going through knowing which one is correct
  269. if i save that for a second and i got my sign in and i push that
  270. and i refresh it because there's no use of fun what i'm expecting to happen is
  271. just to be taken back to my account page and i have brilliant
  272. i'm now going to go onto my account page and in my account page
  273. I'm gonna have a look at what I can do to make sure that I can see that
  274. information at the top here so I've included my header here and what I want to
  275. do is just simply check to see if the submission if my session has
  276. any errors within it so I'm gonna put in place before my
  277. include header because remember session has to be the first thing it sees session
  278. and I want underscore start
  279. so that knows then that that session is on that
  280. page and what I'm going to do is I'm going to go down to where my sign-in form
  281. actually is and in my sign-in form section I'm going to add in a paragraph
  282. i'm going to put it in and say simply php
  283. i want to echo dollar sign underscore session and
  284. the session i want is to echo out not the connection page sorry
  285. my signing error
  286. and i'm going to terminate that and end the php now this will give me
  287. some errors you'll see them
  288. this well it didn't i was expecting to see an error at the top telling me there are
  289. no signing errors but if i submit this now it is refreshing and coming back
  290. and the reason i didn't see the errors when i expected to see the errors was i
  291. forgot to put that site let's go there refresh it
  292. still no errors mind and we're not getting that very strange uh
  293. echo session sign in there
  294. account let's close all of these down let's make sure that i push the wrong page
  295. there we go
  296. and there's my error that's what i expect to see an error on the sign-in page
  297. because something has gone wrong if i put in this details and i put in the password
  298. there and i submit it it says you've entered the wrong user details
  299. so back on here then back on my account page
  300. to it here just to bring the users deep attention to it a bit more
  301. I'm going to add some style to it so I want a span tag for it and I'm going
  302. to style this using the old-fashioned style methods equals
  303. and I'm going to set the color to red nice and simple
  304. now my span is there in place I'm going to take that
  305. and place that at the end here and I've got that set up.
  306. I could go class again and text
  307. center
  308. Let's count, push, refresh that.
  309. You supplied incorrect details.
  310. I've also supplied incorrect element there.
  311. class tt equals
  312. American isn't it center taxos attack
  313. center will double check in a second nope
  314. okay so Dreamweaver
  315. and doesn't want to center that text so I'm just gonna leave that class element out
  316. style
  317. text align
  318. center we'll do it that way but
  319. that way instead
  320. and let's refresh that still doesn't want to center it no matter what i do never
  321. mind and takes the line center there so we've got that error in
  322. place but if i go back to home and i load my account it's going to tell me that i've
  323. entered incorrect details which realistically i haven't there shouldn't be anything
  324. in my session
  325. what i wanted to do is when i go onto my session page here i wanted to start
  326. my session i wanted to display the error and then i want to remove the error from my
  327. session so that it's no longer there no longer to be seen and therefore i don't
  328. have to worry about it so at the very bottom here i've included my footer and then
  329. i'm going to go in the very very last line and i'm going to do session.
  330. And what I want to do is session destroy to get rid of my session afterwards.
  331. So what it should do is load my session, display my error if there is one, and then
  332. destroy the session so then there's no issues.
  333. Copy fail. And that's still coming up with my issue there.
  334. Did I put that?
  335. I'm going to put a post-it note on my screen saying to put.
  336. There you go. Now, this was that error I was talking about earlier.
  337. This error is telling me that there is no session information to be used
  338. and it's trying to access it on line 32, but it doesn't actually exist.
  339. So line 32 is this one here where it's saying to echo it.
  340. I'm going to say, even if I put is set or
  341. that error what i can do instead is i can control the error messages i get presented
  342. with and by controlling those error messages i can actually decide
  343. what is going to be logged to me and what isn't i'm going to go through that one
  344. with you a little bit later on so the the errors aren't really going to cause as
  345. much error at the moment so if i submit that i get that error page if i go back to
  346. it this now says that I'm under find index signing so there it is we can
  347. check but I'm fairly certain that if I put in place on here if
  348. not empty
  349. I did dollar sign underscore session
  350. and I want to see if that
  351. section isn't empty
  352. to see if that section has that key in place and it's going to give me the same
  353. error.
  354. But let's give it a go anyway.
  355. Let's go part of the bell,
  356. count. Oh, that did solve that error.
  357. There we go. It doesn't usually do it, but it's quite happily done it there.
  358. 123 this time.
  359. There we go. And password1234 submit
  360. is taking me to user found.
  361. It's at this point here then that we would take them to their account page, their
  362. personal account page, as opposed to the login page.
  363. Now I've got this page here called account.
  364. and what I'm actually going to do is rename that to login because it makes more
  365. sense.
  366. I'm just going to very quickly change this to login.
  367. I'm going to put the login page up because let's face it I forget that plenty of
  368. times anyway and I'm going to jump into my www file just find the account
  369. page here. Don't want to do that and I'm just going to delete that to remove it.
  370. So I've got that login page there instead.
  371. I just need to go to my header.
  372. And in my header, where the menu is, now bar, I can expand it out and
  373. I can go to
  374. account is actually linking to login.
  375. Now, that's all set up. That's all in place.
  376. I can now log in with my page there instead.
  377. What I'm going to do is open this login section here and it's got my start session.
  378. It's got my if empty element for that element there.
  379. I'm also going to include, I'm going to check to see if I
  380. have a session in place and I'm going to check actually to see if something has been
  381. stored in session specifically.
  382. I want to see if session email has been stored.
  383. So I say grab that from there, session starts.
  384. Before we include anything, let's just double check to see if session email
  385. exists. And if it does, I want to change the header again
  386. to location.
  387. And the location this time around is going to be account.
  388. I know we just deleted account.
  389. We are going to recreate account, but it's going to be my login account page.
  390. So I've got, does my session contain my email?
  391. If it does go to the account page.
  392. So when I come onto here, the logic behind this is,
  393. account doesn't exist now. I know it doesn't.
  394. Let's go back to localhost. If I click onto account, what it's actually going to do
  395. is take me to login. when I go to login login will check to see if
  396. I'm logged in and if so redirect me directly to my account first now my header file
  397. wasn't uploaded properly because as we saw two seconds ago when I hovered over my
  398. menu account didn't say login it said account okay
  399. so that's set up then I'm going to go out of these two.
  400. I'm going to open up my login function.
  401. That's perfectly fine. I'm going to open up my sign-in function and I'm going to use
  402. a similar setup here in this user account found.
  403. So let's highlight that, drop that in place.
  404. It's going to be email.
  405. But this time around, rather than having that set there, I'm going to be referencing
  406. part of this section here, part of my result.
  407. And to do so, what I need to do is create a small loop to go through my result.
  408. There's only one entry in there, but this will allow me to say dollar sign row
  409. equals result, dollar sign result.
  410. And I want to fetch the
  411. associated results.
  412. Now that's going to create for me my loop.
  413. It's got my result here. It's fetching the associated results.
  414. And inside this loop here, what I want to do is set different parameters.
  415. I've told it, I want to check if my email, forename, and surname from users
  416. matches.
  417. here which would then be from my dollar sign row as it's
  418. coming out and using fetch associated it uses names I can simply say email
  419. I can do the same process here and here and instead of email I can do
  420. for name and surname and And
  421. again, copy these elements,
  422. paste them there.
  423. So while I've only got one result, loop through that one result, storing the details
  424. in session, and then redirect me to my account page.
  425. Obviously, this one here now needs to be login, and so does this one here.
  426. Login. Nice and simple. So I've got those in place.
  427. it allows me to sign in and where I'm signed in it will put me in the right place I
  428. just need to bring one more piece of information back and that is whether they are
  429. an admin or not so I've got admin here I could do loan tokens as well but for now
  430. I'm just gonna check is admin so I'm gonna go comma is admin I'm gonna
  431. highlight this section copy it and paste it I'm gonna say is
  432. admin and over here install that
  433. now if I was deciding with the correct details so jjones
  434. password one two three four and submit
  435. it says user found because I forgot to press
  436. it's still saying user found i haven't told it to say user found anyway
  437. sign in put that there refresh that continue
  438. it's trying to go to account but account doesn't exist
  439. so let's just go in very quickly new file account dot
  440. php and save that
  441. Our account.php is going to be a PHP page.
  442. We're going to do session underscore start.
  443. And I'm going to print underscore R simply dollar sign
  444. underscore session to get all of my session
  445. variables out and save that.
  446. If I refresh this page now, no.
  447. put and refresh this page now, we get our variables that we have set where it's
  448. my details and it's in place.
  449. Where I got session start then, I'm going to take apart from this page here and I'm
  450. actually going to do the opposite.
  451. So on account, I'm going to say if session email is empty,
  452. redirect to login.
  453. Now, if I go to my login page for a second, because that clears my session, it has a
  454. delete session at the end there.
  455. If I try to go to account now, because that should be empty,
  456. if empty, I suppose I could just do
  457. session entirely.
  458. So if empty session, I put that
  459. and refresh this. Because session is empty, it takes me back to my
  460. login page. So I can't actually access that page without being logged in.
  461. Again, wrong password, not allowed, right password.
  462. and load in and I can access this page now, which I couldn't two seconds ago.
  463. So that's given me my secure login system to access my database.
  464. I'm providing a very simple element where I'm querying my database to get my user
  465. details. If the user exists and there's only one user with those details, then log
  466. me in, store these details in session and redirect me to a
  467. Otherwise, tell me there's an error, take me back to the login page.
  468. Now while we're here, what I'm going to do is go on my account page.
  469. I'm going to go to my index page.
  470. I'm going to take this top section onto account
  471. and paste it. Don't worry about that error.
  472. I'll fix that in a second. and this bottom section, copy it
  473. and paste it and the only issue we should
  474. have, let's correct
  475. that and that and then I'm going to come out of
  476. PHP that goes back into PHP correctly and then here I want to go back
  477. into PHP.
  478. Here we go. Now if I save that and actually remember to put it for a change what
  479. will happen is when I refresh this page now it's there and it's written in place but
  480. it's just not seen.
  481. On index what do I want? I want my colored div element as well.
  482. Div and Div.
  483. And save that.
  484. Put that.
  485. Refresh the page.
  486. And there we go.
  487. in place. We have our account page here which when we go to it, it takes me to my
  488. login and actually logs me out.
  489. It's a bit of a strange function.
  490. We will address that but right now we have our basic login facility.
  491. The next one up we're going to do is our forgot password.
  492. Quite an easy system but it does involve some emailing elements so we're going to
  493. jump into that in about two seconds time.
  494. Okay so next up we were going to look at forgotten password but after a little bit
  495. of testing doing forgotten password via UwAmp's mail system didn't seem to be sitting
  496. as comfortably as I'd like it to so I'm going to come back to that in a further
  497. video. We're going to go on to register any users so we can sign in and register
  498. users this session and I will come back to forgot password.
  499. A lot of the logic works it's just the mail section side of things that isn't as I
  500. want it to be.
  501. So let's start with our register page then.
  502. So we have our register section here.
  503. In our register page we have our for name, surname, email, password, password
  504. repeat and we have our terms and conditions elements.
  505. What we need to do is set up the register process element which I'm going to call
  506. sign up. So let me right click and go for new file.
  507. I'm going to call it signup.php and let's get rid of that element there.
  508. I open up the sign up it's going to be the similar job get rid of all of that
  509. element and actually from sign in quite a lot of this can be reused.
  510. So I'm going to copy that from my sign in page, paste it here because we still want
  511. to connect here. We still want to have a session started at the top with it and we
  512. still want to check if our email and passwords have been entered.
  513. We also want to put in place a check to see if our forename
  514. and our surname have been entered to make sure that they are there.
  515. And with our passwords
  516. I want my password. I want to double check.
  517. I'm sure I called it password repeat.
  518. I did password repeat and put that in place as well.
  519. The whole name.
  520. Now I am going to put these in place just because syntactically it makes it look a
  521. bit nicer just so I understand what's going on.
  522. It doesn't need a capital R anymore and that's in place.
  523. That will take me back now instead of going back to my login page it should go back
  524. to my register page and I should be able to put that in place
  525. that's my sign up page if I up that can't believe I didn't forget to do that part
  526. it's posting to my sign up page I did change my mind on that name there so
  527. register let's post that up and let's go into my register
  528. here we are submit it it's saying it can't find sign up of course it can't
  529. sign up dot PHP put that there
  530. and it takes me straight back here
  531. it's not giving me the error information because I added that on my login
  532. page and I haven't added that here yet so login let's jump back here we got that
  533. that but we do want to have this span tag here
  534. copy that and sign
  535. register page sorry not sign there we go save that
  536. register let's put that a couple of times just so we're sure of it submit it
  537. And again, it's not giving me my error.
  538. Ah, because I did forget how can we reference anything in sessions if
  539. we don't have our session started at the top there.
  540. There we go. Register page. Put it.
  541. Submit it.
  542. Come on.
  543. So register starts the session.
  544. If the session sign in errors, let's go on to my sign up page.
  545. Session sign in error. Yes, that's there.
  546. Okay, it should be register error, but I'll do that in a second.
  547. Print that there.
  548. So sign in and log in and go. We've got register and sign up.
  549. put that, put that.
  550. There you go, error on this sign-in page.
  551. Need to correct it ever so slightly, but we've got that basic element in place.
  552. On here then, it's not an error on the sign-in page anymore, it's an error on the
  553. register page.
  554. And we're working in sign up, so let's just put that.
  555. Okay, brilliant. We've got all that in place.
  556. Let's correct this before we go any further.
  557. It's not a sign in error, it's a register error.
  558. That one shouldn't need to exist so I can get rid of that.
  559. And on here it is register error, register
  560. error, save that and let's put those two pages up.
  561. And let's just double click it.
  562. Perfect, right, all in place then.
  563. Let's go through and see. Well, I know what happened, but if I put this in, it's
  564. telling me it needs to be an email address.
  565. So one at two dot com, easily faked,
  566. and it's gone through that process.
  567. So on our sign up page then, we have checked for errors.
  568. We haven't gotten any errors, so we're at this stage here.
  569. We didn't redirect to our login page, because I've just got rid of that command, and
  570. I'm gonna get rid of this section here as well.
  571. What I want to do this time around, rather than doing a select, is I want to insert
  572. that information.
  573. I want to insert into users.
  574. Now, I'm capitalizing this. When capitalizing is best practice, it's best practice
  575. as it is. You can or you can't do it.
  576. It doesn't necessarily mean either way is right or wrong.
  577. Realistically, I should have done it the other way around, but I didn't.
  578. My bad. So I'm going to insert into this.
  579. Let's have a look a second. Our email, our password, our forname and surname and is
  580. admin. I'm not putting the town stuff in just yet.
  581. That will come at a later stage.
  582. And so I'm going to insert loan tokens as well.
  583. So let's go comma loan tokens.
  584. And that's there. So I'm inserting into users these values.
  585. then I'm going to go over here and what I'm going to say is values to match
  586. what's going on here
  587. are and I don't need to do the referencing them individually but
  588. what I do need to do is include all of them so values open bracket
  589. this time around I'm going to start off with not email because it will
  590. put the first one into the first one so email should be right actually
  591. going to say for name.
  592. I'm going to do a comma. I'm going to do surname.
  593. And I'm going to do comma.
  594. I got for name, surname. I got is admin.
  595. And let's try and just tidy this up as we're doing it, shall we?
  596. So it can be on multi-line. It doesn't have to all be on one line.
  597. so going like that just works nicely just to separate them out so i can see what i
  598. have and haven't done so comma there i got four email phone name
  599. surname is admin
  600. comma i got password i haven't got up here i will deal with that in a second because
  601. i want to do something slightly different with password i say loan tokens
  602. and actually these two here
  603. going to be different. I'll deal with that in a second.
  604. And then comma, password.
  605. Email will grab from our post.
  606. Forename will grab from our post.
  607. Surname will grab from our post.
  608. Generally speaking, when we add a user to our database, they are going to be a user,
  609. not an admin. So I'm going to put a zero in place.
  610. When we add a new user to our database, they're going to have three loan tokens.
  611. So I'm going to put a three in place.
  612. As these are numeric values, they don't need our speech marks.
  613. They are integers, they are not strings.
  614. Password hasn't been included here then.
  615. Let's correct that. Password.
  616. And have that there. So that's now inserting all of those elements into
  617. the correct place. That should be right there.
  618. Perfect. So there's my SQL command.
  619. If I want to run that, all I've got to do instead of a result is connection,
  620. query,
  621. So if I save this for a second and I have a look here, you can see there's only that
  622. element there. If I refresh this page and resubmit that information and then refresh
  623. my database, we get no changes.
  624. Let's have a look. Why? Probably because of one simple thing.
  625. Sign up. Put.
  626. Put. Now, let's try again. Continue.
  627. And refresh.
  628. Still nothing. Let's see what's going on.
  629. So there's my SQL. Let's go echo
  630. $sql.
  631. Save and put that and refresh.
  632. Enter into users, these values here.
  633. Brilliant. Let's copy that into Heidi.
  634. Run that as an SQL command and see what it tells us.
  635. street name doesn't have a default value.
  636. Right. Okay. So in my data, in my table setup, what I've got here is street
  637. name, no default set already.
  638. I'm going to say I will allow it to be now.
  639. Town and postcode are probably going to give me the same error.
  640. So let's save that and go to my query, run my query.
  641. And now it appears. If I try and run it a second time, mind,
  642. Because the primary key is already taken.
  643. Now, if I go into here and I refresh it, continue, I get no
  644. difference in place there. What I need to do is check to see what happened with my
  645. connection here. So I can say if, and do an if command on it,
  646. my query is triple equal to true.
  647. and what that will do is make sure that it's not only run but if there's any errors
  648. it will let us know what the errors are and then I'm going to say echo
  649. error and to that error I'm gonna put in
  650. dollar sign SQL
  651. there we go so there's my query so that's the SQL command I'm
  652. gonna say
  653. br to go on to the next line and simply then do dollar sign connection
  654. and i want to know the error
  655. so if it works do nothing at the moment if there's an error tell me what that error
  656. is
  657. let's put that up let's run this and it's saying
  658. insert into there there's my query error insert into there let's get rid of that
  659. there it says primary key there's already a primary key for that user that user
  660. already exists within my database realistically that should be the only errors we
  661. get back is the fact that that that primary key already exists so we could say that
  662. the account exists and redirect people to forgot password
  663. For now, I can insert into it.
  664. I don't want to echo that there for a second.
  665. And I've got this section here.
  666. If the user has signed up, I'm not going to take them to a login page, as a lot of
  667. websites do. What I'm going to do is I'm going to use the sign-in function here.
  668. I'm going to take the details from this section here.
  669. And I'm going to emulate what's happening here in the sign-in.
  670. so if there are no errors I don't have to do my while loop but what I do need to
  671. do is say session is equal to and then this time around it's going to be my post
  672. email that's going to be the same thing post underscore
  673. for name and post underscore surname and as they're registering I know
  674. that's a zero so I can set that there.
  675. So let's put that in place. All of that's there.
  676. I don't need that extra brace.
  677. Just tidying up some of these indentations a little bit and put that there.
  678. Okay. So if I
  679. put that again and I go into my data and just simply delete number one,
  680. two for a second, there we go that no longer exists if I go into here
  681. and I go back to register one I'll say two password one
  682. password one submit and it's inserted those values for me
  683. no problem didn't redirect me but if I refresh this
  684. it has inserted it so it's definitely done my insert
  685. there but for some reason it didn't actually redirect me let me save this
  686. and sign up put that again just simply delete this
  687. user a second because i'm thinking it's my error for not
  688. there we go and it's taking me to this page now it's going to give me this sign in
  689. error there's this error on the sign-in page, fair enough, tell me that, but the
  690. email details, it has got my details in place.
  691. I'm not sure why it's got a sign-in error there.
  692. I will deal with that one, but we have that in place.
  693. Password repeat, password repeat, just top and 10 in those.
  694. Now, that is a very basic form of setting up a sign-in.
  695. Password's there, username's there.
  696. I could then go to login and now logged in if I
  697. did 1.2 and my password I said was 1 click sign in
  698. it would sign me in because it's taken the details that I've added in place
  699. it's not the most secure
  700. I don't know how you feel but if I knew that every website I went
  701. on to and I registered with had a database where they could see my username and
  702. password used in plain text, I wouldn't be very impressed.
  703. What we can actually do to try and be a bit more secure
  704. with this is tackle this password element here.
  705. Now, when setting up our passwords, it's always good
  706. practice to hash the password in some way, which just adds an
  707. element of encryption to it.
  708. the hash function in php allows you to do this without having any additional
  709. needs so i'm going to put in a char let's say 512 encryption on my password
  710. and that is to there to put
  711. that in place there now if i was to save this sign up and i was to
  712. post that sign up there delete one two again
  713. and just go back to accounts. I want to register.
  714. One, two. I'm going to put in one and one for the passwords.
  715. Submit them. It's put me in places, registered me, and when I refresh it, this time
  716. around, my password is encrypted.
  717. That one character, number one, has become this long string.
  718. I mean, it's getting better. I would take that as a basic form of encryption
  719. to store the password.
  720. To make it more encrypted I would possibly go down the route of
  721. putting in my email address beforehand and then the password
  722. so that I can put those in place and what that will do is make that level of
  723. security for it a little bit higher.
  724. Now I will have to redo, don't control the D, part of my
  725. login to make sure that it deals with that.
  726. Because at the moment, if I go sign up a second and post that,
  727. what I'm actually going to do is just very quickly take this value here, copy it
  728. into notepad
  729. and paste it. Remember, that is equal to the number one.
  730. I'm going to delete this user
  731. and recreate him a third time.
  732. So let's go account. I'm going to go register.
  733. Again, just using the number one as my password.
  734. I'm going to submit it. It's recreated that user again.
  735. And if I refresh the page,
  736. that is now the reference for number one
  737. the main difference being with this if i go back
  738. into here a second and just undo that hashing just so you can see why it's important
  739. sorry i do want to keep the hashing i don't want the email part there so we know
  740. that by doing this and not having the email in place it will hash it it will create
  741. that password for me i'll leave the user there a second refresh it we know that the
  742. number one was referenced like that i'm going to go back in let's go account i'm
  743. going to register this time um i'm to register as um one
  744. first name one one email one at one
  745. dot com and again one and one and submit it
  746. that's going to create that user for me and the password
  747. because it hasn't had
  748. any form of sort what it's called added to the front of it which is what I use the
  749. username for the email address for the passwords are identical so even though the
  750. password isn't stored in plain text as one it is relatively easy
  751. to see what the same passwords are and again if you were registering
  752. for a malicious service and you found that they were trying to find out people's
  753. passwords it wouldn't be too difficult without some form of salting to
  754. find out what those passwords are
  755. now if i go back into here and i re-add in that hash element so i want to put
  756. email dot before it so it uses the email as well as the password
  757. that's perfectly fine my sign up is perfectly fine i can up that to that user there
  758. but i do need to go into my sign in and where i
  759. I now need to incorporate my element of hashing.
  760. Because right now, the only user who could sign into my database is my
  761. initial user with a password of password1234.
  762. If I go like this, now the only users who can sign into my database after I push it,
  763. as always, would be the two new users.
  764. So even though CPD and Mr. J. Jones is there, If I go to account, I go CPD,
  765. I go password 1234 and submit,
  766. it's accepted that. Let me log out a second and make sure that
  767. sign in is pushed.
  768. There. Okay. So let's go in and go CPD
  769. password1234
  770. submit. It's now telling me they're incorrect details, which is what I'd expect.
  771. So in that regard, I would have to re-register this user.
  772. So I'm going to delete him a second.
  773. I'm going to go in and register that user.
  774. so i'm going to do me james jones email address cpd of mr j
  775. jones password one two three
  776. password one two three submit let's log me in and if we
  777. look at the password for it it is now a much more secure password
  778. if i go to my account page and i go cpd password
  779. one two three four
  780. no because I won't do three one two three and submit
  781. it logs me in is admin is zero I just want to correct that so I
  782. am admin is admin is one and actually when I load this page it's not updated
  783. because I have to log out to log back in to see that change
  784. and I said this one so
  785. within this series then within this session we have created a login system which
  786. allows our users to verify they exist and we've also created a system that will
  787. allow us to register new users as users with secure encrypted
  788. passwords the only thing we haven't done is the forgot password which obviously can
  789. be quite important especially when you forget your password and we will come back to
  790. that one. If I get a solution to it today, I will upload the solution later on this
  791. evening. If not, I'll make sure it's in place by session number eight and it will be
  792. part of session seven. It'll be an additional video put in the bottom there.
  793. Thank you all for watching. Hopefully that makes sense.
  794. I'm anticipating you may need to watch this one through a couple of times, pause it
  795. and see how it's going along. Any problems, don't hesitate to get in contact.
  796. Thank you.
  797. Thank you.