Jump to content

PHP Login problem


Twist3d

Recommended Posts

Hello, well i'm trying to create a simple Admin/Moderator login.

But it oddly enough doesn't work. And i'm usually blind and can't spot obvious things so thats what I think it is.

But can anyone help?

 

<?php
session_start();
if (isset($_SESSION['Mod'])){
echo "Mod";
}else{
if (isset($_SESSION['Admin'])){
echo "Admin";
}else{
echo "Login";
}
}
?>

 

This is just the test, but this is on a Navigation bar, this is supposed to changed it to what session is currently logged.

So if I use this PHP code to login:

 

case 'login2':
session_start();
$password = $_POST['password'];
$sql = "SELECT * FROM passwords WHERE Admin='$password'";
$result = mysql_query($sql);
$count = mysql_num_rows($result);
if($count==1){
session_register("Admin");
echo "<center>Logged in as Administrator</center>";
}else{
$sql2="SELECT * FROM passwords WHERE Moderator='$password'";
$result2 = mysql_query($sql2);
$count2 = mysql_num_rows($result2);
if($count2==1){
session_register("Mod");
echo "<center>Logged in as Moderator</center>";
}else{
echo "<center>Wrong password!</center>";
}
}
break;

 

Then this should log me in as a Mod or Admin, which will changed the Button on the Navbar to either Mod, Admin or Login depending on what session i'm logged in as.

But this doesn't work. I type in the password on the form page, it says on the login2 page (The code above) that i'm logged in as either an admin or mod, but on the page where the NavBar button is supposed to change it remains always "Login".

Any help?

Link to comment
Share on other sites

So the new code is now:

 

<?php
session_start();
if (isset($_SESSION['Mod'])){
echo "Pie";
}else{
if (isset($_SESSION['Admin'])){
echo "Banana";
}else{
echo "Login";
}
}
?>

 

case 'login2':
session_start();
$password = $_POST['password'];
$sql = "SELECT * FROM passwords WHERE Admin='$password'";
$result = mysql_query($sql);
$count = mysql_num_rows($result);
if($count==1){
$_session['Admin'];
echo "<center>Logged in as Administrator</center>";
}else{
$sql2="SELECT * FROM passwords WHERE Moderator='$password'";
$result2 = mysql_query($sql2);
$count2 = mysql_num_rows($result2);
if($count2==1){
$_session['Mod'];
echo "<center>Logged in as Moderator</center>";
}else{
echo "<center>Wrong password!</center>";
}
}
break;

 

Still doesn't work.

Link to comment
Share on other sites

Try This:

case 'login2':
session_start();
$password = $_POST['password'];
$sql = "SELECT * FROM passwords WHERE Admin='$password'";
$result = mysql_query($sql);
$count = mysql_num_rows($result);
if($count==1){
$_session['Admin'] = 'admin';
echo "<center>Logged in as Administrator</center>";
}else{
$sql2="SELECT * FROM passwords WHERE Moderator='$password'";
$result2 = mysql_query($sql2);
$count2 = mysql_num_rows($result2);
if($count2==1){
$_session['Mod'] = 'mod';
echo "<center>Logged in as Moderator</center>";
}else{
echo "<center>Wrong password!</center>";
}
}
break;

 

I take it your page is refreshing after the query is made? If you are using an ajax link then your navbar will not refresh unless you have it set too, and so the navbar will not recheck for the session

 

Link to comment
Share on other sites

Try This:

case 'login2':
session_start();
$password = $_POST['password'];
$sql = "SELECT * FROM passwords WHERE Admin='$password'";
$result = mysql_query($sql);
$count = mysql_num_rows($result);
if($count==1){
$_session['Admin'] = 'admin';
echo "<center>Logged in as Administrator</center>";
}else{
$sql2="SELECT * FROM passwords WHERE Moderator='$password'";
$result2 = mysql_query($sql2);
$count2 = mysql_num_rows($result2);
if($count2==1){
$_session['Mod'] = 'mod';
echo "<center>Logged in as Moderator</center>";
}else{
echo "<center>Wrong password!</center>";
}
}
break;

 

I take it your page is refreshing after the query is made? If you are using an ajax link then your navbar will not refresh unless you have it set too, and so the navbar will not recheck for the session

 

Nope, still doesn't work.

But I don't get exactly what you mean by the refreshing bit.

I don't have any ajax code in my project. And nothing refreshes, the code above is the login and the navbar feature.

Link to comment
Share on other sites

Ok, imagine it like this:

 

Page loads, reads from top to bottom

You login - Does page redirect after this?

Does your redirect reload your navbar?

 

Basically your navbar has already been processed, the code will not be read again unless the navbar is refreshed, so it is basically seeing on 1st run  isset(sessioninfo) = no and continuing, to get it to read yes, it will have to run again, does that make sense?

 

Link to comment
Share on other sites

I tried your way and this way:

 

case 'login2':
session_start();
$password = $_POST['password'];
$sql = "SELECT * FROM passwords WHERE Admin='$password'";
$result = mysql_query($sql);
$count = mysql_num_rows($result);
if($count==1){
$_session['Admin'] = 'Admin';
echo "<center>Logged in as Administrator</center>";
?>
<meta http-equiv="REFRESH" content="0;url=index.php"></HEAD>
<?php
}else{
$sql2="SELECT * FROM passwords WHERE Moderator='$password'";
$result2 = mysql_query($sql2);
$count2 = mysql_num_rows($result2);
if($count2==1){
$_session['Mod'] = 'Mod';
echo "<center>Logged in as Moderator</center>"; 
?>
<meta http-equiv="REFRESH" content="0;url=index.php"></HEAD>
<?php
}else{
echo "<center>Wrong password!</center>";
}
}
break;

 

Both redirected, neither worked.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.