Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7988475
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T12:27:50+00:00 2026-06-04T12:27:50+00:00

I just can’t get to set sessions in php, and I can’t find the

  • 0

I just can’t get to set sessions in php, and I can’t find the reason why.

Hope you can give me a clue, on where I might be doing something wrong!

index.php

<?php
    // Inialize session
    session_start();

    include_once("commons/config.php");
    $authenticated = checkLoggedIn("yes", FALSE);
    //flushMemberSession();

    var_dump($authenticated);

    echo "<pre>";
    var_dump($_SESSION);
    echo "</pre>";
?>
... followed by html and some php ifs

jquery ajax call WHICH RETURNS TRUE or FALSE, as it should, from checkPass() inside actions.php

$('#login').click(function(){
    var data = $('#login-form').serialize();
    $.post('commons/actions.php', data, function(result){
        if(result == true){
            console.log(result);
            //location.reload();
        }else{
            console.log('not authenthicated');
        }
    },'json');
    return false;
});

actions.php

session_start();
if (!$_SERVER['SERVER_ADDR'] == $_SERVER['REMOTE_ADDR']){
    header('HTTP/1.0 400 Unauthorized', true, 400);
    exit;
} else {

    include('config.php');

    $user = mysql_real_escape_string(stripslashes($_POST['username']));
    $pass = mysql_real_escape_string(stripslashes($_POST['password']));

    $response = checkPass($user, $pass); // false or

    $debug = array('user'=>$user, 'pass'=>$pass, 'response'=>$response);

    print_r(json_encode($response));

}

functions:

function checkLoggedIn($status, $redirect=TRUE){
    switch($status){
        case "yes":
            if(!isset($_SESSION["loggedIn"])){
                if($redirect) {
                    header("Location: login.php");
                    exit;
                } else {
                    $authenticated = false;
                    return $authenticated;
                }
            } else {
                checkLoggedIn("no");
            }
        break;
        case "no":
            if(isset($_SESSION["loggedIn"]) && $_SESSION["loggedIn"] === true ){
                //header("Location: members.php");
                $authenticated = true;
                return $authenticated;
            }
        break;
    }   
    return true;
}

function checkPass($username, $password) {
    $query="SELECT username, password FROM users WHERE username='$username' and password='$password'";
    $result=mysql_query($query, $link) or die("checkPass fatal error: ".mysql_error());

// Check exactly one row is found:
if(mysql_num_rows($result)==1) {
    cleanMemberSession($username);
    return true;
    /*$row=mysql_fetch_array($result);
    return $row;*/
}
//Bad username:
return false;
}

function cleanMemberSession($username) {
session_regenerate_id();
$_SESSION["username"]=$username;
$_SESSION["loggedIn"]=true;
session_write_close();
}

UPDATE

AJAX Headers

Response Headers
Connection  Keep-Alive
Content-Length  4
Content-Type    text/html
Date    Sun, 27 May 2012 19:36:54 GMT
Keep-Alive  timeout=5, max=100
Server  Apache/2.2.21 (Win32) mod_ssl/2.2.21 OpenSSL/1.0.0e PHP/5.3.8 mod_perl/2.0.4 Perl/v5.10.1
X-Powered-By    PHP/5.3.8
Request Headers
Accept  application/json, text/javascript, */*; q=0.01
Accept-Encoding gzip, deflate
Accept-Language en-us,en;q=0.5
Connection  keep-alive
Content-Length  31
Content-Type    application/x-www-form-urlencoded; charset=UTF-8
Cookie  PHPSESSID=qhbjq76f4np7iug09jrnl4j5j1
Host    localhost
Referer http://localhost/tw/Tevienes/web/
User-Agent  Mozilla/5.0 (Windows NT 6.1; WOW64; rv:12.0) Gecko/20100101 Firefox/12.0
X-Requested-With    XMLHttpRequest

UPDATE2

BTW, the response to var_dump($_SESSION); is array(0) { }

UPDATE3

I just added $_SESSION['test'] = 'alex'; after session_start(); in index.php, and the session variable has been set… so It must be something with the function setting the variables… or who know what else

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T12:27:55+00:00Added an answer on June 4, 2026 at 12:27 pm

    Put session_start() in all called .php files, also the ones called from ajax.
    Also at the start of actions.php put parentheses on the comparisons. ! operator has higher precedence than < or >.

    change

     if (!$_SERVER['SERVER_ADDR'] == $_SERVER['REMOTE_ADDR']){
    

    to

     if (!($_SERVER['SERVER_ADDR'] == $_SERVER['REMOTE_ADDR'])){
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I just can't figure it out how to set custom validator messages in Zend_Form
I just can't get the ajax service to work. A simple class to $.get(http://google.com)
In theory this seems easy but I just can't get my head around this...
I've been researching but I just can't seem to get it right. I have
I just can't find the option anywhere. Is there some way in eclipse to
I just can't find a simple answer to this simple question I have from
I just can't get into it. No matter what I do... It resulted from
I just can't get this thing. How is ZeroClipboard supposed to work? Why does
I just can't get my scroll view to actually scroll.. Everything displays fine except
I just can't figure this out, for some reason this: $string = #mainparent {

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.