1
我對會話處理感到困惑。我想訪問我的索引頁面,無論用戶是否登錄。但是,如果用戶登錄,那麼我想更換我的登錄名並註銷按鈕。如果沒有登錄,則顯示登錄和註冊菜單。但在這兩種情況下,索引頁都應該可以訪問。 當我在頭中包含會話文件時,索引頁僅顯示爲登錄用戶。如果我只寫session_start();在與頁面頂部檢查我想訪問index.php登錄或不登錄索引頁登錄詳細信息
if(isset $_SESSION['login_user'])
{ echo $login_session
}
//index.php
<?php
$currentPage = 'index';
include_once "header.php"; ?>
\t <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>
\t \t \t \t \t \t \t \t \t <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>
爲了這個,我必須包括session.php文件文件頭和頭是相同的所有它會要求先登錄的文件 –
和什麼是logged_in –
爲什麼不只是使用Session_start(); ?那麼你的sesssion就開始了,你可以在上面的答案中使用它。 – SteffenCH