Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Old 11-29-2012, 11:49 AM Session problems
konetch's Avatar
Ultra Talker

Posts: 288
Trades: 0
Hey guys, I know I've been asking a lot of questions lately, but I really do appreciate all the help I've been getting from the site. I have been trying to build a secure backdoor login to my site. I wanted to have the administrator login to a part of the site that would have a unique id parameter every time he logged in so that random users could never access that part of the site.

I think the problem deals with the sessions in my code, but I'm not sure. Right now when I login it tells me that I have a redirect loop. I'm not sure how this is happening. I tried fixing it with making a for loop around the header function but it still doesn't work the way I want it to. If anybody could just point me in the right direction, I'd appreciate it so much.

PHP Code:
<?php
//Some random requires and functions were up here
session_start();
    if (isset(
$_POST["submit"]))
    {
        
$sUser $_POST["username"];        //Initialize with form's username
        
$sPass md5($_POST["password"]);    //Initialize with form's password
        
$_SESSION['user'] = $sUser;
        
$_SESSION['pass'] = $sPass;
    }
        
    if (
$_SESSION['user'] == "admin")    //Check to make sure only admin can login
    
{
        
$sUser $_SESSION['user'];
        
        
$connect mysql_connect($_PARAM["DatabaseServer"],$_PARAM["DatabaseUser"],$_PARAM["DatabasePass"]);    //Connect to mysql
        
$selectDB mysql_select_db($_PARAM["MainDatabase"],$connect);    //Connect to database
        
        
$check mysql_query("SELECT * FROM users WHERE username = '$sUser'");    //Select row
        
if (!$check)
        {
            echo 
"mysql query error";    //Delete after done fixing the bugs
        
}
        
$INFO  mysql_fetch_array($check);
        if (!
$check)
        {
            echo 
"mysql fetech array error";    //Delete after done ficing the bugs
        
}
        if (
$_SESSION['pass'] == $INFO["password"])    //Check password with the one in the database
        
{
            
$sRandom randomString(150);    //Initialize with a random string of numbers and letters, length 150
            
$sLink $_PARAM["WebsiteAddress"] . "backdoor/login.php?id=" $sRandom;    //Create link
            
for ($_SESSION['passthru'] = 0$_SESSION['passthru'] < 1$_SESSION['passthru']++)
            {
                
header ("Location: " $sLink);    //Redirect the page
            
}
            
$ID $_GET["id"];                //Get the id
            
if ($ID == $sRandom)            //Check id
            
{
                echo 
"You've done it, and logged in!";
            } 
            else 
errorMessage("Sorry, an error occured");
        } 
        else 
errorMessage("Either the username or the password were wrong");
        
        
mysql_close();
    } 
?>
My logic must be flawed somewhere, I just can't figure out where. Thanks
__________________
Alex

Last edited by konetch; 11-29-2012 at 12:04 PM.. Reason: changed original code
konetch is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 11-30-2012, 08:24 AM Re: Session problems
Skilled Talker

Posts: 69
Trades: 0
Could you not restrict access using access levels?

Give admin an access level of 5 and users a lower level.
HullBorn is offline
Reply With Quote
View Public Profile
 
Old 11-30-2012, 10:06 AM Re: Session problems
konetch's Avatar
Ultra Talker

Posts: 288
Trades: 0
I mean I could easily do that, but I already created all of the code and I feel as if something is wrong wit just one line in my code. I just don't know how to get the header to stop redirecting endlessly.
__________________
Alex
konetch is offline
Reply With Quote
View Public Profile
 
Old 11-30-2012, 10:40 AM Re: Session problems
Skilled Talker

Posts: 69
Trades: 0
Try putting this
PHP Code:
 if ($_SESSION['user'] == "admin")    //Check to make sure only admin can login
    
{
        
$sUser $_SESSION['user'];
        
        
$connect mysql_connect($_PARAM["DatabaseServer"],$_PARAM["DatabaseUser"],$_PARAM["DatabasePass"]);    //Connect to mysql
        
$selectDB mysql_select_db($_PARAM["MainDatabase"],$connect);    //Connect to database
        
        
$check mysql_query("SELECT * FROM users WHERE username = '$sUser'");    //Select row
        
if (!$check)
        {
            echo 
"mysql query error";    //Delete after done fixing the bugs
        
}
        
$INFO  mysql_fetch_array($check);
        if (!
$check)
        {
            echo 
"mysql fetech array error";    //Delete after done ficing the bugs
        
}
        if (
$_SESSION['pass'] == $INFO["password"])    //Check password with the one in the database
        
{
            
$sRandom randomString(150);    //Initialize with a random string of numbers and letters, length 150
            
$sLink $_PARAM["WebsiteAddress"] . "backdoor/login.php?id=" $sRandom;    //Create link
            
for ($_SESSION['passthru'] = 0$_SESSION['passthru'] < 1$_SESSION['passthru']++)
            {
                
header ("Location: " $sLink);    //Redirect the page
            
}
            
$ID $_GET["id"];                //Get the id
            
if ($ID == $sRandom)            //Check id
            
{
                echo 
"You've done it, and logged in!";
            } 
            else 
errorMessage("Sorry, an error occured");
        } 
        else 
errorMessage("Either the username or the password were wrong");
        
        
mysql_close();
    } 
within the
PHP Code:
  if (isset($_POST["submit"]))
    {
        
$sUser $_POST["username"];        //Initialize with form's username
        
$sPass md5($_POST["password"]);    //Initialize with form's password
        
$_SESSION['user'] = $sUser;
        
$_SESSION['pass'] = $sPass;

        
// PUT CODE HERE
    

When you have submitted the form, are you redirecting back to the same page?
HullBorn is offline
Reply With Quote
View Public Profile
 
Old 11-30-2012, 06:01 PM Re: Session problems
konetch's Avatar
Ultra Talker

Posts: 288
Trades: 0
ok i fixed it, i put the if ($ID == $sRandom) statement as an else if at the end of the page. Thanks for the help though
__________________
Alex
konetch is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Session problems
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.17588 seconds with 11 queries