不同页面上的php会话

时间:2016-06-02 05:25:59

标签: php session

我是php新手。我正在开发一个网站,并且在不同页面上的会话有一些问题。当我登录网站时,我的标题变为

Main Reklam Logout Anna

当我点击我网站上的其他链接时会发生这种情况

Main Reklam Login Register

但是当我再次点击主要时,会话再次出现

Main Reklam Logout Anna

这是我的header.php

 <?php 
session_start();
?>
<!DOCTYPE html>
<html lang="en">
  <head>
<meta charset="utf-8">


<meta name="viewport" content="width=device-width, initial-scale=1" />

<title>Telefonal</title>

<meta name="description" content="Source code generated using layoutit.com">
<meta name="author" content="LayoutIt!">

<base href="http://domain.com"></base>


<link rel="stylesheet" href="<?php echo $config['template'];?            >assets/css/bootstrap.min.css">
<link rel="stylesheet" href="<?php echo $config['template'];?>assets/css/style.css">
<link rel="stylesheet" href="<?php echo $config['template'];?>assets/css/custom.css">
<link rel="stylesheet" href="<?php echo $config['template'];?>assets/css/font-awesome.min.css">
<link rel="stylesheet" href="//netdna.bootstrapcdn.com/font-awesome/4.0.3/css/font-awesome.css">




<link href='https://fonts.googleapis.com/css?family=Ubuntu' rel='stylesheet' type='text/css'>







</head>
  <body>

<div class="container-fluid" >






<!-- Navigation menu -->

<div class="row"  >
    <div class="col-md-12">
        <nav class="navbar navbar-default top-navigation" role="navigation" >
            <div class="navbar-header">

                <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
                     <span class="sr-only">Toggle navigation</span><span class="icon-bar"></span><span class="icon-bar"></span><span class="icon-bar"></span>
                </button> 
            </div>

            <div class="collapse navbar-collapse " id="bs-example-navbar-collapse-1">
                <ul class="nav navbar-nav navbar-left">
                    <li id="left-first-list">
                        <a href="http://www.domain.com/index.php" ><img src="upload/main/house.png">  Main</a>
                    </li>
                    <li id="left-second-list">
                        <a href="http://domain.com/reklam.html"><img src="upload/main/website-design-symbol.png">    Reklam</a>
                    </li>

                </ul>

                <ul class="nav navbar-nav navbar-right">
                                         <?php if(isset($_SESSION['user']) ){?>
                                             <li id="right-first-list">
                        <a href="http://domain.com/logout.html"><img src="upload/main/login.png">  Logout</a>
                    </li>    <?php                                                  
                                              }else{?>
                                            <li id="right-first-list">
                        <a href="http://domain.com/login.html"><img src="upload/main/login.png">  Login</a>
                    </li>
                                                   <?php
                                                }?>



                                                  <?php if(isset($_SESSION['user'])){?>
                    <li id="right-second-list">
                <a href="#"><img src="upload/main/clipboard-with-pencil.png">  <?php echo $_SESSION['user'];?></a>
                    </li> <?php                                                         
                                                  }else{?>

                    <li id="right-second-list">
                <a href="http://domain.com/register.html"><img src="upload/main/clipboard-with-pencil.png">  Qeydiyyat</a>
                    </li>

                                               <?php    }?>


                </ul>
            </div>

        </nav>
    </div>
</div>

这是我的login.php我正在使用facebook登录

    <?php
    session_start();
    //require 'functions.php'; 
    // added in v4.0.0
    require_once 'dbconfig.php';
    require_once 'autoload.php';
    use Facebook\FacebookSession;
    use Facebook\FacebookRedirectLoginHelper;
    use Facebook\FacebookRequest;
    use Facebook\FacebookResponse;
    use Facebook\FacebookSDKException;
    use Facebook\FacebookRequestException;
    use Facebook\FacebookAuthorizationException;
    use Facebook\GraphObject;
    use Facebook\Entities\AccessToken;
    use Facebook\HttpClients\FacebookCurlHttpClient;
    use Facebook\HttpClients\FacebookHttpable;
    // init app with app id and secret

    FacebookSession::setDefaultApplication( '**********','*************8' );
    // login helper with redirect_uri
    $helper = new FacebookRedirectLoginHelper('http://www.domain.com/fbconfig.php' );
    try {
    $session = $helper->getSessionFromRedirect();
    } catch( FacebookRequestException $ex ) {
    // When Facebook returns an error
    } catch( Exception $ex ) {
    // When validation fails or other local issues
    }
    // see if we have a session
    if ( isset( $session ) ) {
     // graph api request for user data
      $request = new FacebookRequest( $session, 'GET', '/me' );
      $response = $request->execute();
     // get response
      $graphObject = $response->getGraphObject();
  $fbid = $graphObject->getProperty('id');              // To Get Facebook ID
  $fbfullname = $graphObject->getProperty('name'); // To Get Facebook full name
  $femail = $graphObject->getProperty('email');    // To Get Facebook email ID

  /* ---- Session Variables -----*/
    $_SESSION['user_id'] = $fbid;           
    $_SESSION['user'] = $fbfullname;
    $_SESSION['EMAIL'] =  $femail;
   //  checkuser($fuid,$ffname,$femail);


  $query="SELECT * from Users where Fuid='$fbid'";
  $check = $db->query($query);
  $res = $check->num_rows;
  if (empty($res) || $res==0) { // if new user . Insert a new record    
  $query2 = "INSERT INTO Users  VALUES ('". $fbid ."','$fbfullname','$femail')";
  $db->query($query2);
  $query_up="UPDATE Users SET Fuid='$fbid'";
  $db->query($query_up);
  $path="upload/".$fbid."/";
  if (!file_exists($path)) {
  mkdir($path);} 
  } else {   // If Returned user . update the user record   
  $query3 = "UPDATE Users SET Ffname='$fbfullname', Femail='$femail' where Fuid='$fbid'";
  $db->query($query3);
  }


  header("Location: index.php");
   } else {
  $loginUrl = $helper->getLoginUrl();
  header("Location: ".$loginUrl);
  }
  ?>

Detail.php

<?php

  if(isset($_GET['pid'])){
$pid=(int)$_GET['pid'];

  $_SESSION['pid']=$pid;


  $product_query=
  "SELECT * from `ad`,`ad_description` 
  WHERE `ad`.`ad_id`=`ad_description`.`ad_id` 
  AND `ad`.`ad_id`='$pid' 
  AND `ad`.`created_date`< NOW()  
  ORDER BY `ad`.`created_date`  
  Limit 1";

  $get_products_row=$db->query($product_query);
  if($get_products_row->num_rows>0){
while($single_product=$get_products_row->fetch_assoc()){
    $products[]=array(
                'ad_id'=>$single_product['ad_id'],
                'ad_name' =>$single_product['ad_name'],
                'picture'=>$single_product['picture'],
                'price'=>$single_product['price'],
                'ad_description'=>$single_product['ad_description'],
                'created_date'=>$single_product['created_date'],
                'user_id'=>$single_product['user_id'],
                'barter'=>$single_product['barter'],
                'city'=>$single_product['city'],
                'phone'=>$single_product['phone'],


                );
}
  //}
  }else{
   echo "lllllll";
    //header("Location: index.php?page=404");
    } 

  }else{
echo "aaaaaaa";
//header("Location: index.php?page=404");
  }

  require_once($config['template']."detail.php");
  ?>





  <?php
  include_once($config['template']."header.php");
  include_once($config['template']."brand_list.php");
  ?>



  <?php if(isset($products)){ ?>
  <?php foreach ($products as  $product) {
  $image=$product['picture'];
  $image_arr=explode("|*|", $image);

  if (strlen($image_arr[1])!=0) {
$src1="upload/".$product['user_id']."/".$image_arr[1];
  }else{
$src1="upload/main/no_image.gif";
  }
  if (strlen($image_arr[2])!=0) {
$src2="upload/".$product['user_id']."/".$image_arr[2];
  }else{
$src2="upload/main/no_image.gif";
  }
  if (strlen($image_arr[3])!=0) {
$src3="upload/".$product['user_id']."/".$image_arr[3];
  }else{
$src3="upload/main/no_image.gif";
  }
  if (strlen($image_arr[4])!=0) {
$src4="upload/".$product['user_id']."/".$image_arr[4];
  }else{
$src4="upload/main/no_image.gif";
  }

  if ($product['picture']==1) {
$barter="mümkündür";
  }else{
$barter="mümkün deyil";
  }



  $url=$_SERVER['REQUEST_URI'];

  $check_url="SELECT * from counter where page_url='$url'";
  $row=$db->query($check_url);
  if ($row->num_rows == 0) {
  $query="INSERT INTO counter values(NULL,'$url','1')";
  $db->query($query);


  }else{
$query2="UPDATE counter
SET count=count+1
WHERE page_url='$url'";
$db->query($query2);

  }

  $query_select_hits="SELECT count from counter where page_url='$url'";
  $row_for_hit=$db->query($query_select_hits);
  while ($data=$row_for_hit->fetch_assoc()) {
  $hits=$data['count'];
  }
  //$hits=$row_for_hit->num_rows;



   ?>



  <div class="row">
    <div class="col-md-12">
        <div class="row" >
            <div class="col-md-6" >






                <div class="row" >
                    <div class="col-md-12">
                        <table class="table" >
                            <thead>
                                <tr>
  <!--                                      <th>
                                        #
                                    </th> -->
                                    <th>
                                        <?php echo $product['ad_name']; ?>
                                    </th>
  <!--                                      <th>
                                        Payment Taken
                                    </th>
                                    <th>
                                        Status
                                    </th> -->
                                </tr>
                            </thead>
                            <tbody>
                                <tr>

                                    <td>
                                        Model
                                    </td>
                                    <td>
                                        <?php echo $product['ad_name']; ?>
                                    </td>

                                </tr>
                                <tr class="active">

                                    <td>
                                        Barter imkani
                                    </td>
                                    <td>
                                        <?php echo $barter;?>
                                    </td>

                                </tr>
                                <tr >

                                    <td>
                                        Şəhər
                                    </td>
                                    <td>
                                        <?php echo $product['city'];?>
                                    </td>

                                </tr>
                                <tr class="active">

                                    <td>
                                        Dərc olunma tarixi
                                    </td>
                                    <td>
                                        <?php echo $product['created_date'];?>
                                    </td>

                                </tr>
                                <tr >

                                    <td>
                                        Qiymət
                                    </td>
                                    <td>
                                        <?php echo $product['price'];?> AZN
                                    </td>

                                </tr>
                            </tbody>
                        </table>
                    </div>
                </div>

                <div class="row">
                    <div class="col-md-12 elave-melumat">
                        <h3>
                            Baxilib <img style="width:40px;height:40px;border-radius:10px;" src="upload/main/animated.gif" /> <?php echo $hits;?>
                        </h3>
                        <div>
                            <p>


                            </p>
                        </div>
                    </div>
                </div>

                <div class="row">
                    <div class="col-md-12 elave-melumat">
                        <h3>
                            Əlavə məlumat
                        </h3>
                        <div>
                            <p>
                                        <?php echo $product['ad_description'];?>

                            </p>
                        </div>
                    </div>
                </div>

                <div class="row" >
                    <div class="col-md-12 elaqe">
                        <h3>
                            Əlaqə
                        </h3>
                        <div>
                            <p>
                                        <?php echo $product['phone'];?>

                            </p>
                        </div>
                    </div>
                </div>


                <div class="row" >
                    <div class="col-md-12 elaqe">

                        <div id="fb-root"></div>
                        <script>(function(d, s, id) {
                          var js, fjs = d.getElementsByTagName(s)[0];
                          if (d.getElementById(id)) return;
                          js = d.createElement(s); js.id = id;
                          js.src = "//connect.facebook.net/en_US/sdk.js#xfbml=1&version=v2.6&appId=234614743547307";
                          fjs.parentNode.insertBefore(js, fjs);
                        }(document, 'script', 'facebook-jssdk'));
                        </script>

                        <div class="fb-comments" data-href="https://www.facebook.com/telefonalaz-1013127532086903/" data-width="500" data-numposts="3">

                        </div>

                    </div>
                </div>


            </div>


        <div class="col-md-6 allImages" >
                <div class="row" >
                    <div class="col-md-6 detail-picture">

                    <img id="myImg"  alt="Trolltunga, Norway" width="270" height="200" src="<?php echo $src1; ?>"/>

                        <!-- The Modal -->
                        <div id="myModal" class="modal">
                          <span class="close">×</span>
                          <img class="modal-content" id="img01">
                          <div id="caption"></div>
                        </div>

                    <img id="myImg" alt="Trolltunga, Norway" width="270" height="200" src="<?php echo $src2; ?>"/>

                        <!-- The Modal -->
                        <div id="myModal" class="modal">
                          <span class="close">×</span>
                          <img class="modal-content" id="img01">
                          <div id="caption"></div>
                        </div>


                    </div>
                    <div class="col-md-6 detail-picture">

                    <img id="myImg"  alt="Trolltunga, Norway" width="270" height="200" src="<?php echo $src3; ?>"/>

                        <!-- The Modal -->
                        <div id="myModal" class="modal">
                          <span class="close">×</span>
                          <img class="modal-content" id="img01">
                          <div id="caption"></div>
                        </div>

                    <img id="myImg"  alt="Trolltunga, Norway" width="270" height="200" src="<?php echo $src4;?>" />

                        <!-- The Modal -->
                        <div id="myModal" class="modal">
                          <span class="close">×</span>
                          <img class="modal-content" id="img01">
                          <div id="caption"></div>
                        </div>


                    </div>
                </div>

        </div>


  <script>
  // Get the modal
  var modal = document.getElementById('myModal');

  // Get the image and insert it inside the modal - use its "alt" text as a  caption
  var img = document.getElementById('myImg');
  var modalImg = document.getElementById("img01");
  var captionText = document.getElementById("caption");
  img.onclick = function(){
modal.style.display = "block";
modalImg.src = this.src;
modalImg.alt = this.alt;
captionText.innerHTML = this.alt;
  }

  // Get the <span> element that closes the modal
  var span = document.getElementsByClassName("close")[0];

  // When the user clicks on <span> (x), close the modal
  span.onclick = function() { 
modal.style.display = "none";
  }
  </script>



        </div>
    </div>
  </div>
  <?php } }?>
  <!-- end of detail part -->

  <?php
  include_once($config['template']."footer.php");
  ?>

编辑:

我的网站是http://telefonal.az/

1 个答案:

答案 0 :(得分:0)

在其他页面中(如OP提到的那样)请确保在使用$ _SESSION之前有session_start();功能。

例如

<?php

if (session_status() == PHP_SESSION_NONE) {
    session_start();
    /*session is started if you don't write this line can't use $_Session  global variable*/
}
echo $_SESSION['user_id'];