944,089 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 387
  • PHP RSS
Nov 8th, 2009
0

PHP Sessions

Expand Post »
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.
PHP Syntax (Toggle Plain Text)
  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
PHP Syntax (Toggle Plain Text)
  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..
Similar Threads
Reputation Points: 78
Solved Threads: 15
Junior Poster
u8sand is offline Offline
131 posts
since Dec 2008
Nov 8th, 2009
0
Re: PHP Sessions
Try making this default.php
php Syntax (Toggle Plain Text)
  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
php Syntax (Toggle Plain Text)
  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.
Sponsor
Featured Poster
Reputation Points: 410
Solved Threads: 258
Occupation: Genius
cwarn23 is offline Offline
3,004 posts
since Sep 2007
Nov 8th, 2009
0
Re: PHP Sessions
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.
Reputation Points: 78
Solved Threads: 15
Junior Poster
u8sand is offline Offline
131 posts
since Dec 2008
Nov 8th, 2009
0
Re: PHP Sessions
ohhh that ws a silly mistake... i didnt notice it at all when i read ur code fr 1st time.. gud job cwarn !!!

Click to Expand / Collapse  Quote originally posted by u8sand ...
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.
Reputation Points: 13
Solved Threads: 21
Junior Poster
venkat0904 is offline Offline
186 posts
since Oct 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: Retrieve all $_POST values...
Next Thread in PHP Forum Timeline: Warning: mysql_connect()





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC