[Help]User must login to access the pages

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • aquilina
    New Member
    • Dec 2011
    • 6

    [Help]User must login to access the pages

    Below is my code where the link will be generated from database.. when user wanted to access the link they must be logged in.. how to make it?


    Code:
    <div class="h3"><?php echo $servername; ?> Features</div>
    <div class="mainbox">
    <ul id="features">
    <?php
    $con = mysql_connect("localhost","root","");
    if (!$con)
      {
      die('Could not connect: ' . mysql_error());
      }
    
    mysql_select_db("job_seeks", $con);
    
    $result = mysql_query("SELECT * FROM employer_user");
    while($row = mysql_fetch_array($result))
      {
      echo "<li><a href="."?page=job_detail".">".$row['business_name']."</a></li>";
      }
    echo "</tbody>";
    echo "</table>";
    
    mysql_close($con);
    ?>
    </ul>
    </div>
    And this is the code for the page that user needed to login to access this page.. this page is linked with the code at the top

    Code:
    <div class="h3"><?php echo $servername; ?> Features</div>
    <div class="mainbox">
    <ul id="features">
    <?php
    $con = mysql_connect("localhost","root","");
    if (!$con)
      {
      die('Could not connect: ' . mysql_error());
      }
    
    mysql_select_db("job_seeks", $con);
    
    $result = mysql_query("SELECT * FROM employer_user");
    $row = mysql_fetch_array($result);
    ?>
    
    <table cellspacing=0 cellpadding=5>
    		<tr><td class=listtitle colspan=2><center><span class='title2'></span></center></td></tr>
    		<?php
    		echo "
    		<form method=\"POST\">
    		<tr><td class=list align=left><b>Username :</b></td><td class=list> ".$row['username']."<br></td></tr>
    		<tr><td class=list align=left><b>Business Name :</b></td><td class=list> ".$row['business_name']."<br></td></tr>
    		<tr><td class=list align=left><b>Address :</b></td><td class=list> ".$row['address']."<br></td></tr>
    		<tr><td class=list align=left><b>Location :</b></td><td class=list> ".$row['location']."<br></td></tr>
    		<tr><td class=list align=left><b>City/Town :</b></td><td class=list> ".$row['city_town']."<br></td></tr>
    		<tr><td class=list align=left><b>Email :</b></td><td class=list> ".$row['email']."<br></td></tr>
    		<tr><td class=list align=left><b>Phone Number :</b></td><td class=list> ".$row['phone']."<br></td></tr>
    		<tr><td class=list align=left><b>Qualification :</b></td><td class=list> ".$row['qualification']."<br></td></tr>
    		<tr><td class=list align=left><b>Salary :</b></td><td class=list> ".$row['salary']."<br></td></tr>
    		<tr><td class=list align=left><b>Description :</b></td><td class=list> ".$row['company_description']."<br></td></tr>";
    		echo "</td></tr></table>";
    		echo "<p><center><input type=\"submit\" name=\"setting\" value=\"Apply\" onClick=\"parent.location='?page=account'\" />
    				</center>";
    ?>
    </ul>
    </div>
  • AutumnsDecay
    New Member
    • Mar 2008
    • 170

    #2
    I'm not exactly what you're after as your question is pretty vague, but it sounds like you're looking for a login script of sorts.

    A simple login form would look something like this:

    Code:
    <?php
    session_start();
    if((!$_SESSION['auth']) || ($_SESSION['auth'] != 1)){
    
    ?>
    
    <form action="login.php" method="post">
    Username: <input name="username" type="text" />
    Password: <input name="password" type="password" />
    </form>
    
    <?php }
    
    
    else {
        //access to authorized stuff
    }
    
    ?>
    And your login page would look something like this, assuming you're using basic encrypting like md5:

    Code:
    <?php
    
    session_start();
    $username = $_POST['username'];
    $password = md5($_POST['password']);
    
    $result = mysql_query("SELECT * FROM users WHERE username = '$username'");
    
    if(mysql_num_rows($result) > 0){
        $row = mysql_fetch_assoc($result);
        if($row['password'] == $password){
            $_SESSION['auth'] = 1;
            header("Location: index.php");
        }
        else {
            print 'Invalid password.';
        }
    }
    else {
        print 'No users found.';
    }
    ?>
    That's a rough idea for a fairly low security login script.

    Comment

    • aquilina
      New Member
      • Dec 2011
      • 6

      #3
      i have my login and logout script pages... but i wanted to make the script that i given above, user must be logged in to access the above pages... how can i do?

      Comment

      • AutumnsDecay
        New Member
        • Mar 2008
        • 170

        #4
        Set a session variable, and check to see if that session variable exists on each page.

        Login Script:
        Code:
        session_start(); // Start the session
        //All your user data verification for login
        $_SESSION['auth'] = 1; //Create a SESSION variable called 'auth'.
        header("Location: yourpage.php"); //Redirects to yourpage.php after the script has run.
        On each proceeding page (yourpage.php, yourpage2.php, etc):

        Code:
        session_start();
        if((!$_SESSION['auth']) || ($_SESSION['auth'] != 1)){ 
        //If the Session's 'auth' variable isn't set, or if it doesn't equal 1, then:
        
            header("Location: index.php"); //Redirect to the home page if they aren't authorized
        }
        Another thing you can do is authorized information in a public place. Example: If a user isn't logged in, display 'Login or Register', else if a user is logged in, display 'Welcome back!'.

        Code:
        if((!$_SESSION['auth']) || ($_SESSION['auth'] != 1)){
            print 'Login or Register';
        }
        
        else if($_SESSION['auth'] = 1){
            print 'Welcome back!';
        }

        Comment

        • aquilina
          New Member
          • Dec 2011
          • 6

          #5
          See this ss>> http://prntscr.com/4hxgv Above code are stored at the pages folder... and in source folder wheres my login/logout pages are stored. thats mean theres a session_start already called.. And this is my login pages looks like>> http://prntscr.com/4hxl8 my login part already set appear on every pages.. how i gonna to do

          Comment

          • AutumnsDecay
            New Member
            • Mar 2008
            • 170

            #6
            Session variables are domain based, not directory based. If you set "$_SESSION['auth']" to '1' on the login page, and include that session on your proceeding pages, $_SESSION['auth'] will still be set.

            Check this out:



            On the first page I made the $_SESSION['auth'] variable be set to "Wooo!" or something like that. When you click 'Go' underneath that, it directs you to https://www.autumnsolutions.org/stor...age2/index.php.

            You can see on the new page that when I print my 'auth' session variable to the screen, it still says 'Wooo!', and then I added the 'Yay' after it to show that it hit a new page.

            I don't want to sound like a jerk, but are you putting any research effort into this at all, or are you just wanting us to give you the answers you need?

            Comment

            • AutumnsDecay
              New Member
              • Mar 2008
              • 170

              #7
              Another note, when you do "session_start( );" on your proceeding pages, it does NOT overwrite the existing session, it EXTENDS the session on to the new page.

              Comment

              • aquilina
                New Member
                • Dec 2011
                • 6

                #8
                -my problem is solved-

                Comment

                • AutumnsDecay
                  New Member
                  • Mar 2008
                  • 170

                  #9
                  Perfect.

                  If one of my answers solved your problem, please set it as the Answer for this thread. This will allow other users that have the same issue as you to see what the fix was.

                  Thanks.

                  Comment

                  • aquilina
                    New Member
                    • Dec 2011
                    • 6

                    #10
                    where to set it as the thread is answer?

                    Comment

                    • AutumnsDecay
                      New Member
                      • Mar 2008
                      • 170

                      #11
                      Bottom right of all my replies you should see "Choose as Best Answer".

                      Thanks.

                      Comment

                      Working...