开发者

Php, session id control doesnt work?

开发者 https://www.devze.com 2023-04-08 23:24 出处:网络
here is the code drives me crazy:) <?php $deger=0; if (session_id() == \'\') { session_start(); $deger=1;

here is the code drives me crazy:)

<?php
$deger=0;
if (session_id() == '') {
session_start();
$deger=1;
}
else{
$deger=0;
}

$row = mysql_fetch_row($result);
 $counter =$row[2];

if($deger==1){
 $counter--;
}
echo $counter;
?>

This basic code dri开发者_开发知识库ves me crazy. the deger==1 condition always returns true and keeps decrementing the counter. What I would like to do is check the session if the session is new decrement it only once. after that dont decrement the value. Am I missing something here? I am new to php maybe I am missing something.

I look forward to your answers thanks.


i think you have to call session_start(); before any chacks like if (session_id() == '') cause theres really nothing in session_id when session has not been started. this code i one used is working for me (may be not perfect):

session_start();
$user = (isset($_SESSION['user']) && $_SESSION['user'] != '' ? $_SESSION['user'] : null);
if ($user == null) {
//it's a not logged in user
//checking users credentials and if it's ok
$_SESSION['user'] = $uid; //or whatever you want to use to identify a user
} else {
//it's logged in user
}


There is always a user session, but not always a php session. So if you did not do a session_start() yet, you can check that with something like this:

if (isset($_SESSION]['loggedin'])){
    $deger = 0;
} else { 
    $deger = 1;
    session_start(); 
    $_SESSION['loggedin'] = true;
}

Wherever you start the session, also fill the $_SESSION var.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号