problems with sessions and login.php

Reply

Join Date: Nov 2004
Posts: 10
Reputation: cjm771 is an unknown quantity at this point 
Solved Threads: 0
cjm771 cjm771 is offline Offline
Newbie Poster

problems with sessions and login.php

 
0
  #1
Aug 28th, 2006
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
  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
  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. ?>
Reply With Quote Quick reply to this message  
Join Date: Jan 2006
Posts: 53
Reputation: Barnz is an unknown quantity at this point 
Solved Threads: 0
Barnz Barnz is offline Offline
Junior Poster in Training

Re: problems with sessions and login.php

 
0
  #2
Sep 1st, 2006
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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2005
Posts: 1,075
Reputation: digital-ether is just really nice digital-ether is just really nice digital-ether is just really nice digital-ether is just really nice 
Solved Threads: 66
Moderator
digital-ether's Avatar
digital-ether digital-ether is offline Offline
Veteran Poster

Re: problems with sessions and login.php

 
0
  #3
Sep 1st, 2006
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']; ?>" />

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.
www.fijiwebdesign.com - web design and development and fun
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC