PageRenderTime 63ms CodeModel.GetById 13ms RepoModel.GetById 2ms app.codeStats 0ms

/mybookbag/processrequest.php

https://bitbucket.org/s2223902/mybookbag
PHP | 54 lines | 49 code | 5 blank | 0 comment | 6 complexity | dc307d1eef677ccab61102d334323093 MD5 | raw file
  1. <?php
  2. session_start();
  3. include('functions.php');
  4. if(!isset($_POST["submit"])){
  5. header("Location: addfriend.php");
  6. }
  7. else{
  8. $username = "";
  9. $email = "";
  10. if(isset($_POST["username"])){
  11. $username = $_POST["username"];
  12. }
  13. else{
  14. $username = "";
  15. }
  16. if(isset($_POST["email"])){
  17. $email = $_POST["email"];
  18. }
  19. else{
  20. $email = "";
  21. }
  22. echo "username=$username";
  23. echo "email=$email";
  24. $targetid = determineTarget($username,$email); //Get id of targetuser
  25. if(!$targetid){
  26. $_SESSION["addresult"] = "Could not process your request somehow.";
  27. header("Location: addfriend.php");
  28. }
  29. else{
  30. if(verifyRequest($targetid)){ //Check if $id is accepted (if you're not trying to add yourself)
  31. $query = "insert into `friend_of`(`p_id`,`friend_id`,`accepted`) values({$_SESSION["id"]},{$targetid},1)";
  32. $handle = mysql_query($query);
  33. if(!$handle){ //If query failed, create message accordingly
  34. $_SESSION["addresult"] = "Could not process your request somehow. Perhaps you already tried to add this person or you are already friends with him or her.";
  35. }
  36. else{ //If not, create message accordingly
  37. $_SESSION["addresult"] = "Request sent!";
  38. }
  39. }
  40. else{ //If you tried to add yourself, create according message.
  41. $_SESSION["addresult"] = "You cannot add yourself you friendless bastard!";
  42. }
  43. header("Location: friends.php");
  44. }
  45. }
  46. function verifyRequest($targetid){ //Returns a boolean
  47. return($_SESSION["id"]!=$targetid);
  48. }
  49. ?>