944,173 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 3282
  • PHP RSS
Aug 28th, 2006
0

problems with sessions and login.php

Expand Post »
For some strange reason I am having problems with sessions, ainly in safari I believe which is wierd considering php is a server-side programming language. anyways, when a person goes to /cp (control panel) for example, it redirects them to login.php?url=/cp. then they login, the sessions are created, and then it redirects them to /cp. but for some strange reason if there is $_GET['url'], the session is only set for the pages in the /cp directory, even if the $_GET['url'] is in the same directory as login.php. Its strange. can anyone help me out?

Login.php
PHP Syntax (Toggle Plain Text)
  1. <?
  2. ob_start();
  3. session_start();
  4. include ("config.php");
  5.  
  6. ?>
  7.  
  8. <html>
  9.  
  10. <head>
  11. <title>login</title>
  12. <LINK REL=StyleSheet HREF="/style.css" TITLE="main" TYPE="text/css">
  13. </head>
  14.  
  15. <body background="bg.bmp">
  16.  
  17.  
  18.  
  19.  
  20. <?require 'header.php';?>
  21. <h2>Login</h2>
  22.  
  23. <?
  24. if ($logged_in){
  25. echo "you are already logged in!";
  26. }else
  27. {
  28.  
  29. if ($_POST['username'] || $_POST['password'])
  30. {
  31. $dbh=mysql_connect($host, $user, $pass) or die ('I cannot connect to the database because: ' . mysql_error());
  32. mysql_select_db($database, $dbh);
  33.  
  34. $result=mysql_query("SELECT * FROM `members` WHERE `username`='".$_POST['username']."' AND `password`='".$_POST['password']."'") or die ("error in login.php" . mysql_error());
  35.  
  36.  
  37. if (!$_POST['username'] || !$_POST['password']){
  38. echo "<div id='error'>Please fill in all fields</div>";
  39. }
  40.  
  41. else if (mysql_num_rows($result)==0){
  42. echo "<div id='error'>That username/password you entered is incorrect</div>";
  43. }
  44. else
  45. {
  46. if(isset($_POST['rememberme'])){
  47. setcookie("username", $_POST['username'], time()+60*60*24*100, "/");
  48. setcookie("password", $_POST['password'], time()+60*60*24*100, "/");
  49. }
  50. $_SESSION['username']=$_POST['username'];
  51. $_SESSION['password']=$_POST['password'];
  52. session_write_close();
  53. header("location:".$_GET['url']);
  54. exit;
  55. }
  56. }
  57. if (!$_GET['url'])
  58. $_GET['url']="/cp";
  59. ?>
  60. Please enter your username and password to continue
  61. <form method="post" action="/login.php?url=<?echo $_GET['url']?>">
  62. <table border="0">
  63. <tr><td>Username:</td><td><input type="text" name="username" size="20"></td></tr>
  64. <tr><td>Password:</td><td><input type="password" name="password" size="20"></td></tr>
  65.  
  66. <tr><td></td><td><input type="checkbox" name="rememberme">Remember me?</td></tr>
  67. <tr><td></td><td><input type="submit" value="login"></td></tr>
  68. <tr><td></td><td><a href="/forgot.php">forgot password?</a></td></tr>
  69. <tr><td></td><td><a href="/register.php">not registered?</a></td></tr>
  70. </table>
  71. </form>
  72.  
  73. <?
  74. }
  75. include('footer.php');?>
  76. </body>
  77.  
  78. </html>
  79. <?ob_end_flush();?>
config.php
PHP Syntax (Toggle Plain Text)
  1. <?
  2. $host=""; //host
  3. $user=""; //username
  4. $pass=""; //password
  5. $database=""; //db
  6.  
  7. function confirmUser($username, $password){
  8. global $host;
  9. global $user;
  10. global $pass;
  11. global $database;
  12.  
  13.  
  14. if(!get_magic_quotes_gpc()) {
  15. //$username = addslashes($username);
  16. }
  17.  
  18. $dbh=mysql_connect($host, $user, $pass) or die ('I cannot connect to the database because: ' . mysql_error());
  19. mysql_select_db($database, $dbh);
  20.  
  21. $q = "SELECT `password` FROM `members` WHERE `username`= '".$username."'";
  22. $result = mysql_query($q) or die("error in config.php".mysql_error());
  23. if(!$result || (mysql_num_rows($result) < 1)){
  24. return 1; //Indicates username failure
  25. }
  26.  
  27. $dbarray = mysql_fetch_array($result);
  28. $dbarray['password'] = stripslashes($dbarray['password']);
  29. $password = stripslashes($password);
  30.  
  31. /* Validate that password is correct */
  32. if($password == $dbarray['password']){
  33. return 0; //Success! Username and password confirmed
  34. }
  35. else{
  36. return 2; //Indicates password failure
  37. }
  38. }
  39.  
  40. function checkLogin(){
  41. /* Check if user has been remembered */
  42. if(isset($_COOKIE['username']) && isset($_COOKIE['password'])){
  43. $_SESSION['username'] = $_COOKIE['username'];
  44. $_SESSION['password'] = $_COOKIE['password'];
  45. }
  46.  
  47. /* Username and password have been set */
  48. if(isset($_SESSION['username']) && isset($_SESSION['password'])){
  49. /* Confirm that username and password are valid */
  50. if(confirmUser($_SESSION['username'], $_SESSION['password']) != 0){
  51. /* Variables are incorrect, user not logged in */
  52. unset($_SESSION['username']);
  53. unset($_SESSION['password']);
  54. return false;
  55. }
  56. return true;
  57. }
  58. /* User not logged in */
  59. else{
  60. return false;
  61. }
  62. }
  63. global $logged_in;
  64. $logged_in = checkLogin();
  65. function checkAccess(){
  66. if (checkLogin()==true){
  67. global $host;
  68. global $user;
  69. global $pass;
  70. global $database;
  71. $dbh=mysql_connect($host, $user, $pass) or die ('I cannot connect to the database because: ' . mysql_error());
  72. mysql_select_db($database, $dbh);
  73. $result=mysql_query("SELECT `access` FROM `members` WHERE `username`='".$_SESSION['username']."' AND `access`=1") or die (mysql_error());
  74. if (mysql_num_rows($result)==0){
  75. return false;
  76. echo "<script>alert('not enough access')</script>";
  77. }
  78. else{
  79. return true;
  80. }
  81.  
  82. }
  83. else{
  84. return false;
  85. echo "not logged in";
  86. }
  87. }
  88. global $check_access;
  89. $check_access=checkAccess();
  90. ?>
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
cjm771 is offline Offline
10 posts
since Nov 2004
Sep 1st, 2006
0

Re: problems with sessions and login.php

Hello I do not know the answer to your problem but I noticed you are outputting HTML to the browser before calling the header(); function.

You may get errors doing that.
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
Barnz is offline Offline
53 posts
since Jan 2006
Sep 1st, 2006
0

Re: problems with sessions and login.php

Hi cjm771,

In your login form, you're setting the action of the form to:

/login.php?url=<?echo $_GET['url']?>

The form sends its data to the server via HTTP POST and you have a URI string that would usually be sent via a HTTP GET.
It usually works, but maybe Safari isn't sending the url param for some reason?
What you could do is use a hidden field instead of appending it to the url.
<input type="hidden" name="url" value="<?php echo $_POST['url']; ?>" />

Quote ...
but for some strange reason if there is $_GET['url'], the session is only set for the pages in the /cp directory, even if the $_GET['url'] is in the same directory as login.php.
Isnt sessions limited only by domain? It doesnt matter which directory is it.

The frequent problem is if you redirect to say: http://www.example.com/ when you set the session for http://example.com
Browsers treat www.example.com as a different domain from example.com

Its a bit hard to read your code, I suggest you seperate the database connection into a seperate class or function. Maybe just calling functions instead of making database calls in login.php (so that all the functions that call the database are in config.php or a seperate file). Just a suggestion.
Moderator
Reputation Points: 457
Solved Threads: 101
Nearly a Posting Virtuoso
digital-ether is offline Offline
1,250 posts
since Sep 2005

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: unexpected T_STRING
Next Thread in PHP Forum Timeline: Classes In Php





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


Follow us on Twitter


© 2011 DaniWeb® LLC