akd
akd

Reputation: 6758

Php, session id control doesnt work?

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 drives 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.

Upvotes: 0

Views: 150

Answers (2)

k102
k102

Reputation: 8079

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
}

Upvotes: 1

Rene Pot
Rene Pot

Reputation: 24815

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.

Upvotes: 1

Related Questions