Jump to content

[SOLVED] Forced Login


bhavin_85

Recommended Posts

On my site, I have a variable that stores whether or not you're logged in ($loggedIn, clever name huh).

 

The only thing you really need to change would be the path back to the login page if they try and access a page without being logged in.

Sort of like...

 

<?php
// This code is put on the page that you want to protect from viewing without being logged in.
if($loggedIn) {
// html code for the page
} else {
// here is the redirect.
// the javascript handles the redirection.
// tells the window to update it's loction.
?>
<script type="text/javascript">
<!--
window.location = "path/to/page.ext";
-->
</script>
<?php
}
?>

 

Hope that explains it better.

Link to comment
Share on other sites

ok ive got the address going through to the beinging login page...jsut having trouble with page that had the login form on it

 

ive tried this : action="login1.php?url='.$url.'" but its printing login1.php?url='.$url.' in the url

 

any ideas why?

Link to comment
Share on other sites

made a bit of progress....but the url is coming up as this now

 

www.mywebsite.com/%5C%5C%5C'./homes/gy478/web/extrainfo.htm.%5C%5C%5C'

 

the way i ahve done it is passed the url through each page via the url then when it came2 the login page got the url and put it into an invisible text box the....in the text box the value is : \'./homes/gy478/web/extrainfo.htm.\'

 

but on the next pge which processes the login details im gettin the url above....any ideas?

Link to comment
Share on other sites

something like this?

 

# buypage.php #

<?php
    session_start();
    if (!$_SESSION['loggedIn'] ) {
        $_SESSION['returnurl'] = 'buypage.php';
        header("location: login.php");
        exit;
    }
    
    // rest of page
?>

 

# login.php #

<?php
    session_start();
    
    if (isset($_POST['action'])) {
        if (validLogin($_POST['username'], $_POST['pwd'])) {          // or some such function
            $_SESSION['loggedIn'] = true;
            $returnurl = isset($_SESSION['returnurl']) ? $_SESSION['returnurl'] : '';
            if ($returnurl) {
                header("location: $returnurl");
                exit;
            }
            else {
                header("location: index.php");
                exit;        
            }  
        }
        else {
            $_SESSION['loggedIn'] = false ;
            echo "Invalid username or password.";
        }
    }
    
    echo <<<FORM
        <form method='post'>
            Username <input type='text' name='username'> <br/>
            Password <input type='password' name='pwd'>  <br/> 
            <input type='submit' name='action' value='Log in'>
        </form>
FORM;
?>

 

Link to comment
Share on other sites

Barand....ive tried implementing this but the header isnt redirecting

In the second if....there is a header which should redirect but its not redirecting for some reason....any ideas?

<?php
session_start();
include('config.php');
$username = $_POST['username'];	
$password = $_POST['password'];
if ($username != 'admin') {
$sql = "SELECT * FROM regcustomer WHERE username='$username' AND password='$password'";
$query = mysql_query($sql);
$count = mysql_num_rows($query);
if($count==1){
$_SESSION['username'] = $username;
$returnurl = isset($_SESSION['returnurl']) ? $_SESSION['returnurl'] : '';
header("Location: $returnurl");
echo "halo";
}
else {
header("Location:login.php?login=fail");
}
}
else {
$sql2 = "SELECT * FROM regcustomer WHERE username='$username' AND password='$password'";
$query2 = mysql_query($sql2);
$count2 = mysql_num_rows($query2);
if($count2==1){
$_SESSION['username']=$username;
header("Location:admin/adminpage.php");
}
else {
header("Location:login.php?login=fail");
}
}
?> 

 

and it is echoing halo....so i know it is going to the right if

Link to comment
Share on other sites

ive tried header and javascript

neither is working

<?php
session_start();
include('config.php');
$return = $_SESSION['returnurl'];
echo $_SESSION['returnurl'];
$username = $_POST['username'];	
$password = $_POST['password'];
if ($username != 'admin') {
$sql = "SELECT * FROM regcustomer WHERE username='$username' AND password='$password'";
$query = mysql_query($sql);
$count = mysql_num_rows($query);
if($count==1){
$_SESSION['username'] = $username;
$returnurl = isset($_SESSION['returnurl']) ? $_SESSION['returnurl'] : '';
?>
<script type="text/javascript">
<!--
window.location='http://www.xxx.yyyy.zz.uk/~abcde/'$returnurl'';
</script>";
<?
echo "halo";
}
else {
header("Location:login.php?login=fail");
}
}
else {
$sql2 = "SELECT * FROM regcustomer WHERE username='$username' AND password='$password'";
$query2 = mysql_query($sql2);
$count2 = mysql_num_rows($query2);
if($count2==1){
$_SESSION['username']=$username;
header("Location:admin/adminpage.php");
}
else {
header("Location:login.php?login=fail");
}
}
?>

when i used header it told me that i couldnt modify the header

Link to comment
Share on other sites

bhavin, using the Javascript redirect that charlieholder recommended earlier should work just fine.

 

?>
<script type="text/javascript">
<!--
window.location = "<?php echo $returnurl; ?>";
-->
</script>
<?php

 

I made a very slight modification but it should work.

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.