Hi
BIG problem here that im struggeling for like 4 hours
i have 3 pages : index , teachers , classes and in all those 3 php pages i required core.php that has ob_start, session_start and two function , the loggedin function works perfectly BUT the loggedout on the other hand acting weird!
i tried to login in those 3 pages and when i login then switch between pages , session is destroyed automatically, doesn't matter if i login in index and go to classes or i login in teachers and go to index!
when i comment the loggedout part in core.php then i can stay logged in | someone give me gun |
core.php code

<?php
ob_start();
@session_start(); 
$current_file = $_SERVER['SCRIPT_NAME'];
@$http_referer =$_SERVER['HTTP_REFERER'];

function loggedin() {
if(isset($_SESSION['user_id'])&& !empty($_SESSION['user_id'])){
	return true;
}else{
return false;

}
}

function loggedout(){

session_destroy();
 }
?>

this part of the code is where i called the function and its the same in all those 3 pages

    <?php if(loggedin()) { ?>
                      <li role="presentation"><a href="<?php loggedout(); ?>">Logout</a></li>
                           <?php } else {  ?>  
<li role="presentation"><a class="pop-up" href="#" data-val= '<?php include "libs/login.html"; ?>'>login</a></li> <li role="presentation"><a class="pop-up" href="#" data-val= '<?php include "libs/register.html"; ?>'>register</a></li> <?php } ?>

Thanks for anyhelp

    well since i can not edit my post i have to reply to my thread that i figured EVEN when i REFRESH the page , session will get empty!

      Hello, and welcome to PHPBuilder! 🙂

      You can only edit posts within a certain time period after they're posted. 😉 It may be a really short time period for new members ... I'm not a mod, so I have no idea about that.

      There is, generally, no reason to do output buffering in a standard PHP script that needs session support, unless you, for some reason, are producing output prior to the session_start() call.

      Also, there is no use case that I can justify at present for error_suppression on variable assignments, or the session_start() call itself.

      Change your first script thus:

      <?php 
      //ob_start(); 
      session_start();  
      $current_file = $_SERVER['SCRIPT_NAME']; $http_referer = $_SERVER['HTTP_REFERER'];

      ... and see if anything changes.

        i found a solution that i know its not The best but still i had to get it done! noticed when i removed the function loggedout() , the session wont be destortyed. and then i redirect it like this

        <a href="logout.php" >Logout </a>
        

        before it was this

        <a href="<?php loggedout(); ?>" > Logout </a>
        

        i mentioned that i included loggedout()function in core where loggedin() is.
        after removed it , i made a new php called logout.php with the simple code of <?php session_destroy;header('Location:' . $http_refer); ?> and its not solved after two days
        but now i have another question!
        i did even give it a parameter but still it would call the loggedout(); so even when i refreshed the page since core.php was in every page at the top it would destroy it.
        now i dont like this way of redirecting it to another page, how can i have it like the function that i want BUT not to be executed in every page load ?

          pc monk;11052509 wrote:

          i found a solution that i know its not The best but still i had to get it done! noticed when i removed the function loggedout() , the session wont be destortyed. and then i redirect it like this

          <a href="logout.php" >Logout </a>
          

          before it was this

          <a href="<?php loggedout(); ?>" > Logout </a>
          

          The top example would be valid, assuming that a page named "logout.php" exists in/on the same domain/site. The second example would only be valid if the function loggedout() prints a valid page name that exists in/on the same domain/site. You seem a tad-confused with PHP and JavaScript; the former operates on the server and the latter inside the browser.

          i mentioned that i included loggedout()function in core where loggedin() is.
          after removed it , i made a new php called logout.php with the simple code of <?php session_destroy;header('Location:' . $http_refer); ?> and its not solved after two days
          but now i have another question!

          I'll have to assume you meant "session_destroy()" ... you have to have the parentheses for the PHP interpreter to interpret a string of text as a function. And $http_refer would need to be defined:

          <?php
          
          session_destroy();
          $http_refer = $_SERVER['HTTP_REFERER'];
          if (!$http_refer || !strlen($http_refer)) $http_refer = "http://mysite.com/index.php"; // or whatever your home page is named
          header("Location: $http_refer");
          exit;

          i did even give it a parameter but still it would call the loggedout(); so even when i refreshed the page since core.php was in every page at the top it would destroy it.
          now i dont like this way of redirecting it to another page, how can i have it like the function that i want BUT not to be executed in every page load ?

          Don't put it in every page load? Don't put it in core? I'm not sure I'm following you; perhaps it's a linguistic barrier?

          As I mentioned, you seem to be confused between JavaScript and PHP. If you want to log out without loading another page, you will probably need to identify and overwrite the session cookie via JavaScript.

            Write a Reply...