Jump to content

Recommended Posts

Hi
 
I have came across a hopefully small issue
 
I have noticed on my project I am working on, if I sign in as either a candidate, agency or employer, the link changes from login to logout under each dropdown within my menu which I can understand as is cause of the session in the coding etc.
 
what I would like to do is for example is under the candidate menu to show logout only if I am logged in as the candidate but the other two under agency and employer stay as login until I log in as either agency or employer
 
A way I have thought of is in each db table is create a role column and on signup, it automatically inserts the value so if a candidate signs up, the role column has a value of Candidate added in the db and then the same for agency and employer so was seeing if was a way to use that role value to only show logout if that role value is logged in
 
Hope that makes sense
 
Below is my menu coding so far, if need to see any other coding like the signup coding, let me know and will paste it in on here
 
<div id="menu">
<ul>
<a href="index.php"><li>Home</li></a>
  <a href="#"><li>About</li></a>
  <li>Candidates
    <ul>
    <?php  
   if (logged_in() == true) {
echo '<a href="candidates-logout.php"><li>Log Out</li></a>';
} else {
echo '<a href="candidates-login.php"><li>Login</li></a>';
}
 ?>
      <a href="candidates-signup.php"><li>Sign Up</li></a>
    </ul>
  </li>
  <li>Agency
    <ul>
    <?php  
   if (logged_in() == true) {
echo '<a href="agency-logout.php"><li>Log Out</li></a>';
} else {
echo '<a href="agency-login.php"><li>Login</li></a>';
}
 ?>
      <a href="agency-signup.php"><li>Sign Up</li></a>
    </ul>
  </li>
  <li>Employers
    <ul>
       <?php  
   if (logged_in() == true) {
echo '<a href="employers-logout.php"><li>Log Out</li></a>';
} else {
echo '<a href="employers-login.php"><li>Login</li></a>';
}
 ?>
      <a href="employers-signup.php"><li>Sign Up</li></a>
    </ul>
  </li>
  <a href="#"><li>Contact</li></a>
</ul>
</div>

 

Link to comment
https://forums.phpfreaks.com/topic/297293-login-and-logout-links-php-issue/
Share on other sites

Yes that approach seems fine. When the user logs in add the role to the session, and then check it for each menu item.

 

login

$_SESSION['role'] = $roleFromDatabase;
menus

if (logged_in() == true && $_SESSION['role'] == 'candidate') {
    echo '<a href="candidates-logout.php"><li>Log Out</li></a>';
} else {
    echo '<a href="candidates-login.php"><li>Login</li></a>';
}

Hi Guru

 

Thank you for the reply, appreciate it

 

I get the bit about the menu coding but am confused on where to put the session coding in on the candidate login page

 

below is the current coding on the candidate-login.php page

<?php

require_once("functions.php");
	require_once("db-const.php");
	session_start();
	if (logged_in() == true) {
		redirect_to("candidates-profile.php");
	}
	
	$title = "Candidates Login - Recruitment Site";

$pgDesc="";

$pgKeywords="";

include ( 'includes/header.php' );


if (isset($_POST['submit'])) {
	$username = $_POST['username'];
	/*$password = $_POST['password'];*/
	$password = md5($_POST['password']);
	
	// processing remember me option and setting cookie with long expiry date
	if (isset($_POST['remember'])) {	
		session_set_cookie_params('604800'); //one week (value in seconds)
		session_regenerate_id(true);
	} 

	$mysqli = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
	# check connection
	if ($mysqli->connect_errno) {
		echo "<p>MySQL error no {$mysqli->connect_errno} : {$mysqli->connect_error}</p>";
		exit();
	}
	
	$sql = "SELECT * from candidates WHERE username LIKE '{$username}' AND password LIKE '{$password}' LIMIT 1";
	$result = $mysqli->query($sql);
	
	if ($result->num_rows != 1) {
		echo "<p><b>Error:</b> Invalid username/password combination</p>";
	} else {
		// Authenticated, set session variables
		$user = $result->fetch_array();
		$_SESSION['user_id'] = $user['id'];
		$_SESSION['username'] = $user['username'];
		
		// update status to online
		$timestamp = time();
		$sql = "UPDATE candidates SET status={$timestamp} WHERE id={$_SESSION['user_id']}";
		$result = $mysqli->query($sql);
		
		redirect_to("candidates-profile.php?id={$_SESSION['user_id']}");
		// do stuffs
	}
}

if(isset($_GET['msg'])) {
	echo "<p class='msg'>".$_GET['msg']."</p>";
}
?>

<!--CONTENT-->

<div id="column-whole">
<div class="features">
<!-- The HTML login form -->
	<form action="<?=$_SERVER['PHP_SELF']?>" method="post">
		<label>Username:</label> <input type="text" name="username" /><br />
		<label>Password:</label> <input type="password" name="password" /><br />
		Remember me: <input type="checkbox" name="remember" /><br />
		<br /><br />
        <a class="button" href="forgot.php">Forgot Password?</a>        
		<br />
		<input type="submit" name="submit" value="Login" />
	</form>
</div>
</div>

<!--CONTENT-->

<?php include( 'includes/footer.php' ); ?>

Set it right with the other session vars for their username and user_id.

// Authenticated, set session variables
$user = $result->fetch_array();
$_SESSION['user_id'] = $user['id'];
$_SESSION['username'] = $user['username'];
$_SESSION['role'] = $user['role'];
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.