Page 1 of 1

How do I make a forced login?

Posted: Sun Nov 28, 2010 2:26 pm
by Ruths99
Hey all, I found a script for login page online and it sets a false or true value if the user is logged on or not, for example, if he is, the value is true.

All the credit goes to Jpmaster77 and the code can be found at:
http://evolt.org/node/60265/


Now, what would I have to do to re-direct any user who has gone onto th "index.HTML" of my website and has not logged on, what code would I use?

Any help is greatly appreciated and please bear in mind that I don know a lot of PHP. Thanks in advance!

Re: How do I make a forced login?

Posted: Sun Nov 28, 2010 2:53 pm
by NeooeN
If I understand you well, you should do something like that:

Code: Select all

<? 
if($logged_in){
   header "location: http://www.where-to-send-logged-user.com";
}else{
      header "location: http://no-logged-user.com/will/be/send/to/this.page";
}
?>
It should be like that, try it and it won't work as you except then we will see what to do next.

Re: How do I make a forced login?

Posted: Sun Nov 28, 2010 4:12 pm
by Ruths99
Hey, thanks for the quick reply but where exactly should I post that HTML code? The main page with the sign-up button and stuff are in "main.php" but there are many other files which all link into one and relate to each other, such as "database.php" and "useredit.php"

Re: How do I make a forced login?

Posted: Sun Nov 28, 2010 4:24 pm
by NeooeN
I gues you have all files created as in the article you are linking to. If so, then you should have on the beginning of the file you want to script, the include lines. I mean those lines:

Code: Select all

<? 
/* Include Files *********************/
session_start(); 
include("database.php");
include("login.php");
/*************************************/
?>
After that you can past the code I posted in last post. If you do not scripting in php at all, just paste in here the source of file you want to make secure and I'll show you.

Re: How do I make a forced login?

Posted: Sun Nov 28, 2010 4:49 pm
by Ruths99
Hey, thanks again for the quick reply but I used a different "main.php," here it is:

Code: Select all

<?
/**
 * Main.php
 *
 * This is an example of the main page of a website. Here
 * users will be able to login. However, like on most sites
 * the login form doesn't just have to be on the main page,
 * but re-appear on subsequent pages, depending on whether
 * the user has logged in or not.
 *
 * Written by: Jpmaster77 a.k.a. The Grandmaster of C++ (GMC)
 * Last Updated: August 26, 2004
 */
include("include/session.php");
?>

<html>
<title>Jpmaster77's Login Script</title>
<body>

<table>
<tr><td>


<?
/**
 * User has already logged in, so display relevant links, including
 * a link to the admin center if the user is an administrator.
 */
if($session->logged_in){
   echo "<h1>Logged In</h1>";
   echo "Welcome <b>$session->username</b>, you are logged in. <br><br>"
       ."[<a href=\"userinfo.php?user=$session->username\">My Account</a>]   "
       ."[<a href=\"useredit.php\">Edit Account</a>]   ";
   if($session->isAdmin()){
      echo "[<a href=\"admin/admin.php\">Admin Center</a>]   ";
   }
   echo "[<a href=\"process.php\">Logout</a>]";
}
else{
?>

<h1>Login</h1>
<?
/**
 * User not logged in, display the login form.
 * If user has already tried to login, but errors were
 * found, display the total number of errors.
 * If errors occurred, they will be displayed.
 */
if($form->num_errors > 0){
   echo "<font size=\"2\" color=\"#ff0000\">".$form->num_errors." error(s) found</font>";
}
?>
<form action="process.php" method="POST">
<table align="left" border="0" cellspacing="0" cellpadding="3">
<tr><td>Username:</td><td><input type="text" name="user" maxlength="30" value="<? echo $form->value("user"); ?>"></td><td><? echo $form->error("user"); ?></td></tr>
<tr><td>Password:</td><td><input type="password" name="pass" maxlength="30" value="<? echo $form->value("pass"); ?>"></td><td><? echo $form->error("pass"); ?></td></tr>
<tr><td colspan="2" align="left"><input type="checkbox" name="remember" <? if($form->value("remember") != ""){ echo "checked"; } ?>>
<font size="2">Remember me next time     
<input type="hidden" name="sublogin" value="1">
<input type="submit" value="Login"></td></tr>
<tr><td colspan="2" align="left"><br><font size="2">[<a href="forgotpass.php">Forgot Password?</a>]</font></td><td align="right"></td></tr>
<tr><td colspan="2" align="left"><br>Not registered? <a href="register.php">Sign-Up!</a></td></tr>
</table>
</form>

<?
}

/**
 * Just a little page footer, tells how many registered members
 * there are, how many users currently logged in and viewing site,
 * and how many guests viewing site. Active users are displayed,
 * with link to their user information.
 */
echo "</td></tr><tr><td align=\"center\"><br><br>";
echo "<b>Member Total:</b> ".$database->getNumMembers()."<br>";
echo "There are $database->num_active_users registered members and ";
echo "$database->num_active_guests guests viewing the site.<br><br>";

include("include/view_active.php");

?>


</td></tr>
</table>


</body>
</html>
How would I add the code to that?

Re: How do I make a forced login?

Posted: Sun Nov 28, 2010 5:45 pm
by Jonah Bron
Probably right after this:

Code: Select all

include("include/session.php");

Re: How do I make a forced login?

Posted: Mon Nov 29, 2010 1:01 pm
by NeooeN
Hey! The code you have posted is diferent then the code in the article. It looks strange to me/ :dubious: Are you sure it works well for now without your modifications?

Re: How do I make a forced login?

Posted: Mon Nov 29, 2010 4:14 pm
by Ruths99
Ye, I used a different piece of code by the same guy:
http://www.evolt.org/node/60384

The code works fine for me now

Re: How do I make a forced login?

Posted: Mon Nov 29, 2010 4:26 pm
by NeooeN
Ok, the thing is still pretty easy. Find include line (in your case it's a very first php code line) and after that paste the code I've gave you but this time change the if line to this:

Code: Select all

if($session->logged_in){
Is it clear now?