根据用户会话显示内容 PHP

Displaying content based on user session PHP

* 我已经解决了这个问题。感谢您的帮助 /*

我正在尝试根据 user_id / 用户名向不同的用户显示一些图像。

我正在尝试使用 userSession 来实现。

我尝试使用此代码:

 <?php

if ($_SESSION['user_id'] == 'Charlie') {
      // do admin
   <li  class="span4 gallery-item" data-id="id-1">
      <img class="fancybox" src="img/photoshop2_small.jpg" data-big="img/photoshop2.jpg" /> </li>
        }

   ?>

我也尝试了 "userSession" 和 "user_email"。 None 成功了。

当我输入上面的代码时,它甚至无法加载我的网站。当我删除它时,它会完美加载。

这是我的 home.php class,它是在用户登录时加载的。它对所有 atm 都是一样的,但我试图只向用户显示某些图像,具体取决于他们是谁。他们没有特殊的等级或任何东西。

<?php
session_start();
include_once 'dbconnect.php';

if(!isset($_SESSION['userSession']))
{
 header("Location: index2.php");
}

$query = $MySQLi_CON->query("SELECT * FROM users WHERE user_id=".$_SESSION['userSession']);
$userRow=$query->fetch_array();
$MySQLi_CON->close();
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <!-- CSS
    ================================================== -->
    <link href='http://fonts.googleapis.com/css?family=Oswald' rel='stylesheet' type='text/css'>
    <link rel="stylesheet" href="css/bootstrap.css">
    <link rel="stylesheet" href="css/bootstrap-responsive.css">
    <!--<link rel="stylesheet" href="css/prettyPhoto.css" />-->
    <!--<link rel="stylesheet" href="css/flexslider.css" />-->
    <link rel="stylesheet" href="css/custom-styles.css">

    <!--[if lt IE 9]>
    <script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
    <link rel="stylesheet" href="css/style-ie.css"/>
    <![endif]-->

    <!-- Favicons
    ================================================== -->
    <link rel="shortcut icon" href="img/favicon.ico">
    <link rel="apple-touch-icon" href="img/apple-touch-icon.png">
    <link rel="apple-touch-icon" sizes="72x72" href="img/apple-touch-icon-72x72.png">
    <link rel="apple-touch-icon" sizes="114x114" href="img/apple-touch-icon-114x114.png">



    <!--Fancybox dwuser.com-->
    <link rel="stylesheet" type="text/css" media="screen" href="http://cdnjs.cloudflare.com/ajax/libs/fancybox/1.3.4/jquery.fancybox-1.3.4.css" />
    <style type="text/css">
        a.fancybox img {
            border: none;
            -o-transform: scale(1,1); -ms-transform: scale(1,1); -moz-transform: scale(1,1); -webkit-transform: scale(1,1); transform: scale(1,1); -o-transition: all 0.2s ease-in-out; -ms-transition: all 0.2s ease-in-out; -moz-transition: all 0.2s ease-in-out; -webkit-transition: all 0.2s ease-in-out; transition: all 0.2s ease-in-out;
        }
        a.fancybox:hover img {
            position: relative; z-index: 999; -o-transform: scale(1.03,1.03); -ms-transform: scale(1.03,1.03); -moz-transform: scale(1.03,1.03); -webkit-transform: scale(1.03,1.03); transform: scale(1.03,1.03);
        }
    </style>

    <!--Fancybox end-->


    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Test |  <?php echo $userRow['user_email']; ?></title>

    <link href="bootstrap/css/bootstrap.min.css" rel="stylesheet" media="screen">
    <link href="bootstrap/css/bootstrap-theme.min.css" rel="stylesheet" media="screen">

    <link rel="stylesheet" href="css/style.css" type="text/css" />
</head>
<body>


<div class="color-bar-1"></div>
<div class="color-bar-2 color-bg"></div>

<div class="container main-container">

    <div class="row header"><!-- Begin Header -->

        <!-- Logo
        ================================================== -->
        <div class="span5 logo">
            <a href="index.htm"><img src="img/%20black.png" alt="" /></a>

        </div>

        <!-- Main Navigation
        ================================================== -->
        <div class="span7 navigation">
            <div class="navbar hidden-phone">

                <ul class="nav">


                    <li><a href="index.htm">Hjem</a></li>


                    <li class="dropdown">
                        <a class="dropdown-toggle" data-toggle="dropdown" href="gallery.html">Galleri <b class="caret"></b></a>
                        <ul class="dropdown-menu">
                            <li><a href="gallery.html">Galleri</a></li>
                            <li><a href="wedding.html">Bryllup</a></li>
                            <li><a href="portrait.html">Portræt</a></li>
                            <li><a href="outside.html">Udendørs</a></li>
                            <li><a href="andet.html">Andet</a></li>
                            <!--<li><a href="gallery-single.htm"></a></li>-->
                        </ul>
                    </li>



                    <li><a href="page-contact.htm">Kontakt</a></li>
                </ul>


                    <!-- Username and logout button .... nav bar only -->
                <ul class="nav navbar-nav navbar-right">
                    <li><a href="#"><span class="glyphicon glyphicon-user"></span>&nbsp; <?php echo $userRow['user_name']; ?></a></li>
                    <li><a href="logout.php?logout"><span class="glyphicon glyphicon-log-out"></span>&nbsp; Logout</a></li>
                </ul>

            </div>


        </div>

    </div><!-- End Header -->


    <div class="row headline"><!-- Begin Headline -->

        <!-- Page Content
        ================================================== -->
        <div class="row">

            <!-- Gallery Items
            ================================================== -->
            <div class="span12 gallery">

                <div class="row clearfix">
                    <ul class="gallery-post-grid holder">

                        <h6>Tryk på billederne for at forstørre dem og for at opnå bedre kvalitet</h6>






                           <li  class="span4 gallery-item" data-id="id-1">
                            <img class="fancybox" src="img/photoshop2_small.jpg" data-big="img/photoshop2.jpg" />
                            </li>




                            <li  class="span4 gallery-item" data-id="id-2">
                                <img class="fancybox" src="img/IMG_2205-Edit-1_small.jpg" data-big="img/IMG_2205-Edit-1.jpg" />
                            </li>


                    </ul>
                </div>


            </div><!-- End gallery list-->

        </div><!-- End container row -->

    </div> <!-- End Container -->

    <!-- Footer Area
        ================================================== -->
    <div class="row"><!-- Begin Bottom Section -->



    </div><!-- End Bottom Section -->

</div> <!-- End Container -->





<!-- Scroll to Top -->
<div id="toTop" class="hidden-phone hidden-tablet">Back to Top</div>

<!-- JS
   ================================================== -->
<script src="http://code.jquery.com/jquery-1.8.3.min.js"></script>
<!--<script src="js/jquery.easing.1.3.js"></script>-->
<script src="js/bootstrap.js"></script>
<!--<script src="js/jquery.prettyPhoto.js"></script>-->
<!--<script src="js/jquery.quicksand.js"></script>-->
<!--<script src="js/jquery.custom.js"></script>-->


<!--Fancybox script-->
<script type="text/javascript" src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
<script type="text/javascript" src="http://code.jquery.com/jquery-migrate-1.2.1.min.js"></script>
<script type="text/javascript" src="http://cdnjs.cloudflare.com/ajax/libs/fancybox/1.3.4/jquery.fancybox-1.3.4.pack.min.js"></script>
<script type="text/javascript">
    $(function($){
        var addToAll = false;
        var gallery = true;
        var titlePosition = 'inside';
        $(addToAll ? 'img' : 'img.fancybox').each(function(){
            var $this = $(this);
            var title = $this.attr('title');
            var src = $this.attr('data-big') || $this.attr('src');
            var a = $('<a href="#" class="fancybox"></a>').attr('href', src).attr('title', title);
            $this.wrap(a);
        });
        if (gallery)
            $('a.fancybox').attr('rel', 'fancyboxgallery');
        $('a.fancybox').fancybox({
            titlePosition: titlePosition
        });
    });
    $.noConflict();
</script>

</body>
</html>

尝试使用此代码代替您的代码:

<?php

if ($_SESSION['user_id'] == 'Charlie') { ?>
      // do admin
   <li  class="span4 gallery-item" data-id="id-1">
      <img class="fancybox" src="img/photoshop2_small.jpg" data-big="img/photoshop2.jpg" /> </li>


<?php } ?>

编辑 如果用户 ID 是唯一的并且不依赖于字母大小写你可以做

if (strtolower($_SESSION['user_id']) === 'charlie') 

您从未更改示例代码中的会话数据。你应该这样做:

$_SESSION['user_id'] = $userRow['user_id']

设置相关会话变量