Jump to content


Photo

Login System on PHP 5 (Help Please)


  • Please log in to reply
5 replies to this topic

#1 bigdaddykraven

bigdaddykraven
  • Members
  • PipPip
  • Member
  • 10 posts

Posted 23 September 2006 - 12:57 AM

Hey Everyone, I have a bit of a problem. I had this script I put together from a mixture of other scripts and modified it to where it would work on my roleplaying character site. When I had to move to a new host, I went from a post 4.1 PHP server to a PHP 5 server. Prior to the switch, the session_register command was being used and as you know, it no longer supports that with globals turned off.

So tried to change it to $_SESSION ['login']; yet it still gives me the same result. What happens when you log in correctly, it simply loads the login page once more. I have the pages set to where you go to them, it checks for the logged in session and then if not, will HEADER revert to the login page. The problem is, I don't think it's keeping the session when I login. It's sending me to the posting page, but saying I'm not logged in and pushing me back to the login page.

Funny thing is, if you put in an incorrect password, it registers as being incorrect and will direct you to the OOPS, page, proving that it is in the reading of the session. I'll include the scripts:

Protected Page
<?php
session_start();
if($login=="") {
Header("Location: login.php");
} else {
?>
Page Contents go below this.
<?php
}
?>

Login Function
<?php
session_start();
$db_host="localhost"; // Your mySQL host, normally localhost
$db_user="mydbhere"; // Your database username
$db_pass="mypasswordhere"; // Your database password
$db="mydbhere"; // the database you are trying to connect to
$connection=mysql_connect ("$db_host", "$db_user", "$db_pass") or die ('I cannot connect to the database because: ' . mysql_error());
mysql_select_db ("$db");

$username=$_POST["username"];
$pw=$_POST["password"];

$q="SELECT * from users where username='$username' and password='$pw'";
$result= mysql_query($q, $connection) or die
("Could not execute query : $q." . mysql_error());

if (mysql_num_rows($result) == 0)
{

echo "<div align=center><b>Oops! Your login is wrong. Please click back and try again.</b></div>";

}
else
{
$r=mysql_fetch_array($result);
$login=$r["username"];
$_SESSION["login"];
//session_register("login"); //This is the old script that I replaced because register_globals are off
Header("Location: roleplaypost.php"); // Place the location of the file you want to go to.
}

?>

I don't want to enable the globals (I don't have that option, .htaccess crashes my website when I put in the coding to allow for this. If anyone wants to check it out, I can set up a false user password/login. Any and all help would be great, thank you.

#2 corbin

corbin
  • Staff Alumni
  • Advanced Member
  • 8,129 posts

Posted 23 September 2006 - 01:08 AM

<?php
session_start();
if($login=="") {
Header("Location: login.php");
} else {
?>
Page Contents go below this.
<?php
}
?>

nothing is setting $login... Try making it a session variable like $_SESSION['login'] and do the same for the login script...
Why doesn't anyone ever say hi, hey, or whad up world?

#3 bigdaddykraven

bigdaddykraven
  • Members
  • PipPip
  • Member
  • 10 posts

Posted 23 September 2006 - 01:17 AM

$_SESSION['login'];

I tried setting it just before the if($login==""); if you look it is already set in the login function script, at the bottom. Also, in the bottom of the login function, it sets $login=$r["username"];

Strange because it worked fine as is (although with session_register("login"); instead of $_SESSION).

When I made the change I mentioned above, it continued to do the same thing.

#4 corbin

corbin
  • Staff Alumni
  • Advanced Member
  • 8,129 posts

Posted 23 September 2006 - 02:02 AM

$_SESSION["login"];
Does absolutly nothing... change that to
$_SESSION['login'] = "logged";
and change
<?php
session_start();
if($login=="") {
Header("Location: login.php");
} else {
?>
Page Contents go below this.
<?php
}
?>
to
<?php
session_start();
$login = $_SESSION['login'];
if($login == "logged") {
Header("Location: login.php");
} else {
?>
Page Contents go below this.
<?php
}
?>

Why doesn't anyone ever say hi, hey, or whad up world?

#5 bigdaddykraven

bigdaddykraven
  • Members
  • PipPip
  • Member
  • 10 posts

Posted 23 September 2006 - 02:37 AM

It still keeps throwing me back to the login page. I'm going to have a play with it for a bit and see if I can find what part is exactly the problem... IF I get anywhere or fail miserably, I'll let you know. Thanks for your help.

#6 bigdaddykraven

bigdaddykraven
  • Members
  • PipPip
  • Member
  • 10 posts

Posted 23 September 2006 - 02:51 AM

Ok, I got it working. It was set to be == logged when it should have been != logged. Thanks for all of your help, I much appreciate it :)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users