简体   繁体   中英

i want to access index.php either logged in or not but with login detail on index page

I am confused with session handling. I want to access my index page either user has logged in or not. But if a user is logged in, then i want to replace my login and sign up buttons with logout. If not logged in then show login and sign up menus. But in both cases index page should be accessible. When i include session file in header then index page is only being shown to logged-in user. And if i write only session_start(); at the top of page and check

if(isset $_SESSION['login_user'])
{  echo $login_session
}

 //index.php <?php $currentPage = 'index'; include_once "header.php"; ?> <div class="stle"> <h1> welcome to home page</h1> //menus //slider // posts </div> <?php include_once "footer.php"; ?> 

 //session.php <?php include('dbconn.php'); session_start(); $user_check = $_SESSION['login_user']; /* $ses_sql = mysqli_query($conn,"select name,email from signup_work where email = '$user_check' ");*/ $ses_sql = mysqli_query($conn,"SELECT name,email FROM signup_hire WHERE signup_hire.email = '$user_check' UNION SELECT name,email FROM signup_work WHERE signup_work.email = '$user_check' "); $row = mysqli_fetch_array($ses_sql,MYSQLI_ASSOC); /*printf("Error: %s\\n", mysqli_error($conn)); exit();*/ $login_session = $row['name']; if(!isset($_SESSION['login_user'])){ header("location:login.php"); } ?> 

 // header.php <?php session_start(); $login_session=""; ?> <!DOCTYPE html> <!--[if IE 8]> <html lang="en" class="ie8 no-js"> <![endif]--> <!--[if IE 9]> <html lang="en" class="ie9 no-js"> <![endif]--> <!--[if !IE]><!--> <html lang="en"> <!--<![endif]--> <!-- BEGIN HEAD --> <head> <meta charset="utf-8" /> <title>Job Monster </title> <meta content="width=device-width, initial-scale=1.0" name="viewport" /> <meta name="description" content="" /> <meta name="keywords" content=""> <meta name="author" content="" /> <meta name="MobileOptimized" content="320"> <!--srart theme style --> <link href="css/main.css" rel="stylesheet" type="text/css" /> <!-- end theme style --> <!-- favicon links --> <link rel="shortcut icon" type="image/png" href="images/favicon.png" /> </head> <body> <!--Loader Start --> <div class="mj_preloaded"> <div class="mj_preloader"> <div class="lines"> <div class="line line-1"></div> <div class="line line-2"></div> <div class="line line-3"></div> </div> <div class="loading-text">LOADING</div> </div> </div> <!--Loader End --> <div class="mj_header"> <div class="container"> <div class="row"> <div class="col-lg-3 col-md-3 col-sm-12 col-xs-12"> <div class="mj_logo"> <a href="index.html"><img src="images/logo.png" class="img-responsive" alt="logo"> </a> <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#mj_menu" aria-expanded="false"> <span class="sr-only">MENU</span> <span class="icon-bar"></span> <span class="icon-bar"></span> <span class="icon-bar"></span> </button> </div> </div> <div class="col-lg-9 col-md-9 col-sm-12 col-xs-12"> <div class="collapse navbar-collapse mj_navmenu" id="mj_menu"> <ul class="nav navbar-nav"> <li class="<?php if($currentPage =='index'){echo 'active';}?>"><a href="index.php">home</a> </li> <li><a href="#">Jobs</a> <ul class="sub_menu"> <li><a href="jobs.html">Jobs</a> </li> <li><a href="jobs_location.html">location</a> </li> <li><a href="job_detail.html">Job Detail</a> </li> </ul> </li> <li class="<?php if($currentPage =='post_job'){echo 'active';}?>"><a href="post_job.php">Post a Job</a> </li> <li><a href="#">Post a Resume</a> <ul class="sub_menu"> <li><a href="post_resume.html">Post a Resume</a> </li> <li><a href="resume_preview.html">Resume Preview</a> </li> </ul> </li> <li><a href="#">Candidates</a> <ul class="sub_menu"> <li><a href="candidates.html">Candidates</a> </li> <li><a href="candidates_detail.html">Candidate detail</a> </li> </ul> </li> <li ><a href="#">Pages</a> <ul class="sub_menu"> <li><a href="blog_fullwidth.html">blog fullwidth</a> </li> <li><a href="blog_sidebar.html">blog sidebar</a> </li> <li><a href="blog_single.html">blog single</a> </li> <li><a href="gallery.html">gallery</a> </li> <li><a href="contact.html">contact</a> </li> <li><a href="signup.php">sign up</a> </li> <li><a href="login.php">login</a> </li> <li><a href="error.html">error</a> </li> </ul> </li> </ul> <ul class="nav navbar-nav navbar-right mj_right_menu mj_withoutlogin_menu"> <li class="mj_searchbtn"><a href="#"><i class="fa fa-search"></i></a> <div class="mj_search_option"> <form> <div class="form-group"> <div class="input-group"> <div class="input-group-addon"><i class="fa fa-search"></i> </div> <input type="text" class="form-control" placeholder="Type and Hit Enter"> </div> </div> </form> </div> </li> <?php if (isset($_SESSION['login_user'])) { ?> <!-- Details --> <div style="float:right; padding-left: 60px; color:#00fd36;"> <li>Welcome<br> <?php echo $login_session; ?></li> <p><a href = "logout.php">Sign Out</a></p></div> <!-- END Details --> <?php } else { ?> <!-- Details --> <li class="<?php if($currentPage =='signup'){echo 'active';}?>"><a href="signup.php"><i class="fa fa-lock"></i> Sign Up</a> </li> <li class="<?php if($currentPage =='login'){echo 'active';}?>"><a href="login.php" data-target="#myModal2"><i class="fa fa-user"></i> Login</a> </li> <!-- END Details --> <?php } ?> </ul> </div> </div> </div> </div> </div> 

Set a session if user is successfully login like:

session_start();

$_SESSION['logged_in'] = true;

and check it like:

if(isset($_SESSION['logged_in']))
{
    // show logout button here
}
else
{
    // show login button here
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM