By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,994 Members | 2,036 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,994 IT Pros & Developers. It's quick & easy.

[Help]User must login to access the pages

P: 6
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?


Expand|Select|Wrap|Line Numbers
  1. <div class="h3"><?php echo $servername; ?> Features</div>
  2. <div class="mainbox">
  3. <ul id="features">
  4. <?php
  5. $con = mysql_connect("localhost","root","");
  6. if (!$con)
  7.   {
  8.   die('Could not connect: ' . mysql_error());
  9.   }
  10.  
  11. mysql_select_db("job_seeks", $con);
  12.  
  13. $result = mysql_query("SELECT * FROM employer_user");
  14. while($row = mysql_fetch_array($result))
  15.   {
  16.   echo "<li><a href="."?page=job_detail".">".$row['business_name']."</a></li>";
  17.   }
  18. echo "</tbody>";
  19. echo "</table>";
  20.  
  21. mysql_close($con);
  22. ?>
  23. </ul>
  24. </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

Expand|Select|Wrap|Line Numbers
  1. <div class="h3"><?php echo $servername; ?> Features</div>
  2. <div class="mainbox">
  3. <ul id="features">
  4. <?php
  5. $con = mysql_connect("localhost","root","");
  6. if (!$con)
  7.   {
  8.   die('Could not connect: ' . mysql_error());
  9.   }
  10.  
  11. mysql_select_db("job_seeks", $con);
  12.  
  13. $result = mysql_query("SELECT * FROM employer_user");
  14. $row = mysql_fetch_array($result);
  15. ?>
  16.  
  17. <table cellspacing=0 cellpadding=5>
  18.         <tr><td class=listtitle colspan=2><center><span class='title2'></span></center></td></tr>
  19.         <?php
  20.         echo "
  21.         <form method=\"POST\">
  22.         <tr><td class=list align=left><b>Username :</b></td><td class=list> ".$row['username']."<br></td></tr>
  23.         <tr><td class=list align=left><b>Business Name :</b></td><td class=list> ".$row['business_name']."<br></td></tr>
  24.         <tr><td class=list align=left><b>Address :</b></td><td class=list> ".$row['address']."<br></td></tr>
  25.         <tr><td class=list align=left><b>Location :</b></td><td class=list> ".$row['location']."<br></td></tr>
  26.         <tr><td class=list align=left><b>City/Town :</b></td><td class=list> ".$row['city_town']."<br></td></tr>
  27.         <tr><td class=list align=left><b>Email :</b></td><td class=list> ".$row['email']."<br></td></tr>
  28.         <tr><td class=list align=left><b>Phone Number :</b></td><td class=list> ".$row['phone']."<br></td></tr>
  29.         <tr><td class=list align=left><b>Qualification :</b></td><td class=list> ".$row['qualification']."<br></td></tr>
  30.         <tr><td class=list align=left><b>Salary :</b></td><td class=list> ".$row['salary']."<br></td></tr>
  31.         <tr><td class=list align=left><b>Description :</b></td><td class=list> ".$row['company_description']."<br></td></tr>";
  32.         echo "</td></tr></table>";
  33.         echo "<p><center><input type=\"submit\" name=\"setting\" value=\"Apply\" onClick=\"parent.location='?page=account'\" />
  34.                 </center>";
  35. ?>
  36. </ul>
  37. </div>
  38.  
  39.  
  40.  
  41.  
Dec 7 '11 #1

✓ answered by AutumnsDecay

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:

www.autumnsolutions.org/storage/sessions

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 http://www.autumnsolutions.org/stora...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?

Share this Question
Share on Google+
10 Replies


AutumnsDecay
100+
P: 170
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:

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. session_start();
  3. if((!$_SESSION['auth']) || ($_SESSION['auth'] != 1)){
  4.  
  5. ?>
  6.  
  7. <form action="login.php" method="post">
  8. Username: <input name="username" type="text" />
  9. Password: <input name="password" type="password" />
  10. </form>
  11.  
  12. <?php }
  13.  
  14.  
  15. else {
  16.     //access to authorized stuff
  17. }
  18.  
  19. ?>
  20.  
  21.  
And your login page would look something like this, assuming you're using basic encrypting like md5:

Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3. session_start();
  4. $username = $_POST['username'];
  5. $password = md5($_POST['password']);
  6.  
  7. $result = mysql_query("SELECT * FROM users WHERE username = '$username'");
  8.  
  9. if(mysql_num_rows($result) > 0){
  10.     $row = mysql_fetch_assoc($result);
  11.     if($row['password'] == $password){
  12.         $_SESSION['auth'] = 1;
  13.         header("Location: index.php");
  14.     }
  15.     else {
  16.         print 'Invalid password.';
  17.     }
  18. }
  19. else {
  20.     print 'No users found.';
  21. }
  22. ?>
  23.  
That's a rough idea for a fairly low security login script.
Dec 7 '11 #2

P: 6
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?
Dec 8 '11 #3

AutumnsDecay
100+
P: 170
Set a session variable, and check to see if that session variable exists on each page.

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

Expand|Select|Wrap|Line Numbers
  1. session_start();
  2. if((!$_SESSION['auth']) || ($_SESSION['auth'] != 1)){ 
  3. //If the Session's 'auth' variable isn't set, or if it doesn't equal 1, then:
  4.  
  5.     header("Location: index.php"); //Redirect to the home page if they aren't authorized
  6. }
  7.  
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!'.

Expand|Select|Wrap|Line Numbers
  1. if((!$_SESSION['auth']) || ($_SESSION['auth'] != 1)){
  2.     print 'Login or Register';
  3. }
  4.  
  5. else if($_SESSION['auth'] = 1){
  6.     print 'Welcome back!';
  7. }
  8.  
Dec 8 '11 #4

P: 6
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
Dec 8 '11 #5

AutumnsDecay
100+
P: 170
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:

www.autumnsolutions.org/storage/sessions

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 http://www.autumnsolutions.org/stora...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?
Dec 8 '11 #6

AutumnsDecay
100+
P: 170
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.
Dec 8 '11 #7

P: 6
-my problem is solved-
Dec 8 '11 #8

AutumnsDecay
100+
P: 170
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.
Dec 8 '11 #9

P: 6
where to set it as the thread is answer?
Dec 8 '11 #10

AutumnsDecay
100+
P: 170
Bottom right of all my replies you should see "Choose as Best Answer".

Thanks.
Dec 8 '11 #11

Post your reply

Sign in to post your reply or Sign up for a free account.