Jump to content

How to restrict some links to be click without valid login


vishalonne

Recommended Posts

I already have the php code for login and varification done using mysql database.

I have some links which should not work if user click them without VALID LOGIN.

My index.html page contain menu -

Home        Computer Science    Informatics Practices      Take Test (login required)        Software      Register      Get Together(login required)

Structure of my web site

index.html---- Login Box and Register Page Link

      Computer Science (Menu)

              XI (Sub Menu)

                    Unsolved Question Papers (Link) login not required

                    Project Samples (Link) login not required

                    Solved Materials (Link) login required

                    Forum (Link) login required

              XI I (Sub Menu)

                    Unsolved Question Papers (Link) login not required

                    Project Samples (Link) login not required

                    Solved Materials (Link) login required

                    Forum (Link) login required

 

Here is the code -

login.php (login form)

<script type="text/javascript" src="sha512.js"></script>  // contain encryption code
<script type="text/javascript">
function formhash(form, password) {
   // Create a new element input, this will be out hashed password field.
   var p = document.createElement("input");
   // Add the new element to our form.
   
   p.name = "p";
   p.type = "hidden"
   p.value = hex_sha512(password.value);
   // Make sure the plaintext password doesn't get sent.
   password.value = "";
   // Finally submit the form.
   form.appendChild(p);
   form.submit();
}
</script>
<?php
if(isset($_GET['error'])) { 
   echo 'Error Logging In!';
}
?>
</head>
<body><form action="process_login.php" method="post" name="login_form">
   Email: <input type="text" name="email" /><br />
   Password: <input type="password" name="password" id="password"/><br />
   <input type="button" value="Login" onclick="formhash(this.form, this.form.password);" />
</form>
</body>

 

process_login.php (checking validity)

 

<?php
define("HOST", "localhost"); // The host you want to connect to.
define("USER", "root"); // The database username.
define("PASSWORD", ""); // The database password. 
define("DATABASE", "check1"); // The database name.

$mysqli = new mysqli(HOST, USER, PASSWORD, DATABASE);
echo "Process Login";
include 'functions.php';
sec_session_start(); // Our custom secure way of starting a php session. 

if(isset($_POST['email'], $_POST['p'])) { 
   $email = $_POST['email'];
   $password = $_POST['p']; // The hashed password.
   if(login($email, $password, $mysqli) == true) {
      // Login success
      echo 'Success: You have been logged in!';
   } else {
      // Login failed
      header('Location: ./login.php?error=1');
   }
} else { 
   // The correct POST variables were not sent to this page.
   echo 'Invalid Request';
}
?>

 

Can see the online demo of web site here http://www.cbsecsnip.in/

Link to comment
Share on other sites

Thank you Pikachu for looking into my Issue.

What I understand from your reply that I should check every page like this

<?php
if(isset($_SESSION['user'])){ ?>
<div id="nav" class="image002-03">
        <span id="smalltext" 
            style="bottom: 0px; margin-bottom: 0px; padding-bottom: 0px; font-family: Calibri; font-size: large; text-align: center;">Service Menu</span>
        <ul id="ul1" class="serviceul">
            <li class="serviceli"><a href="unsolvedCSQPXI.php">Unsolved Question Papers</a></li>
            <li class="serviceli"><a href="unsolvedCSSPXI.php">Unsolved Sample Paper</a></li>
            <li class="serviceli"><a href="#">Notes</a></li>
            <li class="serviceli"><a href="prosamCSXI.php">Projects Samples</a></li>
            <li class="serviceli"><a href="#">Presentations</a></li>
            <li class="serviceli"><a href="#">Uploads</a></li>
            <li class="serviceli"><a href="downloads.php">Solved Materials</a></li>
            <li class="serviceli"><a href="#">Forum</a></li>
            <li class="serviceli"><a href="#">Live Chat</a></li>        </ul>
</div>
<?php
}
else{
do the required login here... (this contains the links to push to a login page)
}?>

 

But if you see the page on site on a page I have 5-6 link some of them need login and some are not how can I separate them?

Link to comment
Share on other sites

As Pikachu said, you don't want to check if the users are logged in before they click the link. You want to check it, at the server, when the page is loading. That way it's easy to determine whether or not someone has access to the page.

If the user doesn't have access, redirect to login form. As simple as that.

Link to comment
Share on other sites

Thanx for guidance. If you don't mind can you please check my code just I tried have some problem

login.php

<script type="text/javascript" src="sha512.js"></script>
<script type="text/javascript">
function formhash(form, password) {

   // Create a new element input, this will be out hashed password field.
   var p = document.createElement("input");
   // Add the new element to our form.
   
   p.name = "p";
   p.type = "hidden"
   p.value = hex_sha512(password.value);
   // Make sure the plaintext password doesn't get sent.
   password.value = "";
   // Finally submit the form.
   form.appendChild(p);
   form.submit();

}
</script>
<?php
if(isset($_GET['error'])) { 
   echo 'Error Logging In!';
}
?>

</head>

<body><form action="[b]process_login.php[/b]" method="post" name="login_form">
   Email: <input type="text" name="email" /><br />
   Password: <input type="password" name="password" id="password"/><br />
   <input type="button" value="Login" onclick="formhash(this.form, this.form.password);" />
</form>

 

process_login.php

<?php
define("HOST", "localhost"); // The host you want to connect to.
define("USER", "root"); // The database username.
define("PASSWORD", ""); // The database password. 
define("DATABASE", "check1"); // The database name.

$mysqli = new mysqli(HOST, USER, PASSWORD, DATABASE);
echo "Process Login";
include 'functions.php';
sec_session_start(); // Our custom secure way of starting a php session. 

if(isset($_POST['email'], $_POST['p'])) { 
   $email = $_POST['email'];
   $password = $_POST['p']; // The hashed password.
   if(login($email, $password, $mysqli) == true) {
      // Login success
      include("XICS.php");
   } else {
      // Login failed
      header('Location: ./login.php?error=1');
   }
} else { 
   // The correct POST variables were not sent to this page.
   echo 'Invalid Request';
}
?>

 

XICS.php on top of the page

<?php
$mysqli = new mysqli(HOST, USER, PASSWORD, DATABASE);
echo "Process Login";
sec_session_start();
?>

 

then where the links should appear

<?php
if(login_check($mysqli) == true){ ?>
<div id="nav" class="image002-03">
        <span id="smalltext" 
            style="bottom: 0px; margin-bottom: 0px; padding-bottom: 0px; font-family: Calibri; font-size: large; text-align: center;">Service Menu</span>
        <ul id="ul1" class="serviceul">
            <li class="serviceli"><a href="unsolvedCSQPXI.php">Unsolved Question Papers</a></li>
            <li class="serviceli"><a href="unsolvedCSSPXI.php">Unsolved Sample Paper</a></li>
            <li class="serviceli"><a href="#">Notes</a></li>
            <li class="serviceli"><a href="prosamCSXI.php">Projects Samples</a></li>
            <li class="serviceli"><a href="#">Presentations</a></li>
            <li class="serviceli"><a href="#">Uploads</a></li>
            <li class="serviceli"><a href="downloads.php">Solved Materials</a></li>
            <li class="serviceli"><a href="forum.php">Forum</a></li>
            <li class="serviceli"><a href="#">Live Chat</a></li>        </ul>
</div>
<?php
}
else{   ?>

<div id="nav" class="image002-03">
        <span id="smalltext" 
            style="bottom: 0px; margin-bottom: 0px; padding-bottom: 0px; font-family: Calibri; font-size: large; text-align: center;">Service Menu</span>
        <ul id="ul1" class="serviceul">
            <li class="serviceli"><a href="unsolvedCSQPXI.php">Unsolved Question Papers</a></li>
            <li class="serviceli"><a href="unsolvedCSSPXI.php">Unsolved Sample Paper</a></li>
            <li class="serviceli"><a href="#">Notes</a></li>
            <li class="serviceli"><a href="prosamCSXI.php">Projects Samples</a></li>
            <li class="serviceli"><a href="#">Presentations</a></li>
            <li class="serviceli"><a href="login.php">Uploads</a></li>
            <li class="serviceli"><a href="login.php">Solved Materials</a></li>
            <li class="serviceli"><a href="login.php">Forum</a></li>
            <li class="serviceli"><a href="#">Live Chat</a></li>        </ul>
</div>
<?php } ?>

 

This is working but if give wrong password or id it is not showing those link which doesn't require login credentials. Rest is fine :-\ :confused:

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.