PHP Sessions

Reply

Join Date: Dec 2008
Posts: 117
Reputation: u8sand is on a distinguished road 
Solved Threads: 15
u8sand's Avatar
u8sand u8sand is offline Offline
Junior Poster

PHP Sessions

 
0
  #1
33 Days Ago
Hello guys,
I'm creating my own website-it's coming along very well but I'm stuck. There is a login, so that you can login to your account. Each account has an access of 1-10, if your access is 0 you are not logged in. When you put your username and password it checks all of the accounts in the mySQL database. That all works fine, but along with this is a forum, if you are logged in you can post ext.. in the forum, if your not logged in you can only view it. I made it so you can view it but how would i have a variable that worked through all the pages in my website so the forum could say do they have an access of greater than 0?
So i thought... Sessions

My default.php runs includes the different things which include each other and it all works out so that its always "technically" on default.php.
  1. <?php
  2. // Start the session
  3. session_start();
  4. $_SESSION['Username'] = "";
  5. $_SESSION['Access'] = 0;
  6. ?>
  7. <html xmlns="http://www.w3.org/1999/xhtml" >
  8. <head>
  9. <title>Website</title>
  10. </head>
  11. <body>
  12. <table width="100%" height="100%" border="0">
  13. <tr height="1%">
  14. <td>
  15. <?php require "menubar.php"; ?>
  16. </td>
  17. </tr>
  18. <tr height="10%">
  19. <td>
  20. <?php require "logo.php"; ?>
  21. </td>
  22. </tr>
  23. <tr valign="top">
  24. <td>
  25. <?php require "main.php"; ?>
  26. </td>
  27. </tr>
  28. </table>
  29. </body>
  30. </html>

I'm not sure if I just don't know how to use Session variables or I'm using them wrong.

here is login.php
  1. <html>
  2. <body>
  3. <?php
  4. if(strlen($_POST['user']) == 0 || strlen($_POST['pass']) == 0)
  5. {
  6. echo "
  7. <center>
  8. <form action=\"?page=Login\" method=\"post\">
  9. <table border=\"0\" width=\"75%\">
  10. <tr>
  11. <td>
  12. Username:
  13. </td>
  14. <td>
  15. <input type=\"text\" name=\"user\" />
  16. </td>
  17. </tr>
  18. <tr>
  19. <td>
  20. Password:
  21. </td>
  22. <td>
  23. <input type=\"password\" name=\"pass\" />
  24. </td>
  25. </tr>
  26. <tr>
  27. <td>
  28. <input type=\"submit\" value=\"Login\" />
  29. <td>
  30. </tr>
  31. </table>
  32. </form>
  33. </center>
  34. ";
  35. }
  36. else
  37. {
  38. $mysql_host = "";
  39. $mysql_database = "";
  40. $mysql_user = "";
  41. $mysql_password = "";
  42.  
  43. $con = mysql_connect($mysql_host,$mysql_user,$mysql_password);
  44. if(!$con)
  45. {
  46. die('Could not connect: ' . mysql_error());
  47. }
  48.  
  49. mysql_select_db($mysql_database,$con);
  50.  
  51. $result = mysql_query("SELECT * FROM Accounts");
  52. while($row = mysql_fetch_array($result))
  53. {
  54. if($row['Username'] == $_POST['user'] && $row['Password'] == $_POST['pass'])
  55. {
  56. $_SESSION['Username'] = $row['Username'];
  57. $_SESSION['Access'] = $row['Access'];
  58. echo "<a href=?page=UserCP>You have successfully logged in click here to access User CP.</a>";
  59. }
  60. }
  61. }
  62. ?>
  63. </body>
  64. </html>

Thanks and i hope you guys can help me..
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 1,498
Reputation: cwarn23 has a spectacular aura about cwarn23 has a spectacular aura about cwarn23 has a spectacular aura about 
Solved Threads: 136
cwarn23's Avatar
cwarn23 cwarn23 is offline Offline
Nearly a Posting Virtuoso
 
0
  #2
33 Days Ago
Try making this default.php
  1. <?php
  2. // Start the session
  3. session_start();
  4. if (empty($_SESSION['Username']) || !isset($_SESSION['Username'])) {
  5. $_SESSION['Username'] = "";
  6. $_SESSION['Access'] = 0;
  7. }
  8. ?>
  9. <html xmlns="http://www.w3.org/1999/xhtml" >
  10. <head>
  11. <title>Website</title>
  12. </head>
  13. <body>
  14. <table width="100%" height="100%" border="0">
  15. <tr height="1%">
  16. <td>
  17. <?php require "menubar.php"; ?>
  18. </td>
  19. </tr>
  20. <tr height="10%">
  21. <td>
  22. <?php require "logo.php"; ?>
  23. </td>
  24. </tr>
  25. <tr valign="top">
  26. <td>
  27. <?php require "main.php"; ?>
  28. </td>
  29. </tr>
  30. </table>
  31. </body>
  32. </html>

And this login.php
  1. <?php
  2. session_start();
  3. ?><html>
  4. <body>
  5. <?php
  6. if(strlen($_POST['user']) == 0 || strlen($_POST['pass']) == 0)
  7. {
  8. echo "
  9. <center>
  10. <form action=\"?page=Login\" method=\"post\">
  11. <table border=\"0\" width=\"75%\">
  12. <tr>
  13. <td>
  14. Username:
  15. </td>
  16. <td>
  17. <input type=\"text\" name=\"user\" />
  18. </td>
  19. </tr>
  20. <tr>
  21. <td>
  22. Password:
  23. </td>
  24. <td>
  25. <input type=\"password\" name=\"pass\" />
  26. </td>
  27. </tr>
  28. <tr>
  29. <td>
  30. <input type=\"submit\" value=\"Login\" />
  31. <td>
  32. </tr>
  33. </table>
  34. </form>
  35. </center>
  36. ";
  37. }
  38. else
  39. {
  40. $mysql_host = "";
  41. $mysql_database = "";
  42. $mysql_user = "";
  43. $mysql_password = "";
  44.  
  45. $con = mysql_connect($mysql_host,$mysql_user,$mysql_password);
  46. if(!$con)
  47. {
  48. die('Could not connect: ' . mysql_error());
  49. }
  50.  
  51. mysql_select_db($mysql_database,$con);
  52.  
  53. $result = mysql_query("SELECT * FROM Accounts");
  54. while($row = mysql_fetch_array($result))
  55. {
  56. if($row['Username'] == $_POST['user'] && $row['Password'] == $_POST['pass'])
  57. {
  58. $_SESSION['Username'] = $row['Username'];
  59. $_SESSION['Access'] = $row['Access'];
  60. echo "<a href=?page=UserCP>You have successfully logged in click here to access User CP.</a>";
  61. }
  62. }
  63. }
  64. ?>
  65. </body>
  66. </html>
I will have to macgyver a login tutorial which reminds me I need to upload that other video tutorial tonight. Hope that helps.
Try not to bump 10 year old threads as it can be really annoying.
http://syntax.cwarn23.net/
Smilies: ^_* +_+ v_v -_- *~*`
My favourite PC. - MacGyver Fan
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 117
Reputation: u8sand is on a distinguished road 
Solved Threads: 15
u8sand's Avatar
u8sand u8sand is offline Offline
Junior Poster
 
0
  #3
33 Days Ago
worked tyvm, so i knew how to do it but i had to include the session start over every page that uses the session. Thanks.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 185
Reputation: venkat0904 is an unknown quantity at this point 
Solved Threads: 19
venkat0904's Avatar
venkat0904 venkat0904 is offline Offline
Junior Poster
 
0
  #4
33 Days Ago
ohhh that ws a silly mistake... i didnt notice it at all when i read ur code fr 1st time.. gud job cwarn !!!

Originally Posted by u8sand View Post
worked tyvm, so i knew how to do it but i had to include the session start over every page that uses the session. Thanks.
Gimme reputation points if u find my post helpful.
use [code] tags wherever applicable
dont start a new thread unless u cant find the topic already on forum.
mark a thread "solved" as soon as u get a solution
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC