<?php

    //The fields and values to insert

  $user = array(
            'users_screen_name'=>$vars ->screen_name,
            'users_profile_image_url'=>$vars -> profile_image_url,
            'users_name'=>$vars->name,
            'users_statuses_count'=>$vars->statuses_count,
            'users_location'=>$vars->location,
            'users_description'=>$vars->description,
            'users_oauth_access_token'=>$access_token['oauth_token'],
            'users_oauth_access_token_secret'=>$access_token['oauth_token_secret']
        );

        // chek existing 
         $check_column = 'users_id';

         $exists = $database->exists( 'useres', $check_column,  array('users_screen_name' => $vars ->screen_name ));


        if (!$exists) {
            $add_query = $database -> insert('useres', $user);
            if ($add_query) {
                //update informations 


                    include 'get_user_info.php';


            }
            }
        else {

            $rs = "SELECT * FROM useres where users_screen_name ='".$vars ->screen_name."'";                        

            $user =  $database -> get_results($rs);
            // add the informations to the session 
            foreach ($user as $key => $value) {
                foreach ($value as $key => $value) {
                    $_SESSION[$key] = $value;
                }                           
            }
            //foreach ($_SESSION as $key => $value) {
                //echo $key ." = > ".$value."<br />";
            //}


            header ('location: user/dashboard.php');
            exit();
    }
    ?>

Dani AI

Generated

Good troubleshooting so far — a couple of focused checks will get this to a clean, reliable fix.

The headers already sent problem being “fixed” by ob_start() means output was being produced somewhere and the buffer simply hid it. Reproduce the original header error (remove ob_start()), read the error text carefully — PHP tells you the file and line where output began. Inspect that file and any includes (especially get_user_info.php) for stray whitespace, a UTF‑8 BOM, or accidental echo/print. Those are the usual culprits that buffering can mask.

The new Invalid argument supplied for foreach() is usually a symptom that the database call returned nothing or a non-traversable value. Check the query and the return type from your DB wrapper: confirm the table name is correct (the code shows useres which looks like a typo), run the SQL directly in your DB client, and verify you actually get a row. If you expect one row, you don’t need a nested loop — take the first row and map fields to session keys explicitly. Also avoid reusing the same loop variable names in nested loops; that will silently corrupt your data assignment (good point from ).

Make sure a session is started before assigning to $_SESSION, and only once (wrap with a session-status guard or centralize session_start). Don’t leave debugging prints or token values in logs — those oauth_token values are sensitive. Final checklist: remove buffering and fix the output source, confirm SQL/table and returned type, ensure session is started, use distinct loop/variable names or direct assignment for a single row, then redirect with a proper Location header and exit.

Recommended Answers

All 6 Replies

well this things dosen't work :

1- space before removed ?>
2- no hidden chars
3- opt codes dosen't work
4- .ini server file too

any other solutions ???

Is the error message saying headers are sent on line header ('location: user/dashboard.php'); or is the error being thrown from dashboard.php?

Usually I get that error when I session_start(); on the target file. If that is the case, you're probably calling session_start(); on both files and since there might be an include somewhere the session can't be started twice.

I'm just trying to guess here, since the code posted isn't enough for me to see where the error is.

If the case I mentioned is the problem you might want to wrap your session_start(); call with an headers_sent(); condition so it doesn't resend headers, like so:

    //if headers not sent
    if (!headers_sent()) {
        //start session
        session_start();
    }
    //continue with the logic since session will already exist

Hope this helps^^

Hi,

If you are getting error like "headers already sent" mens you are showing something before header('location: user/dashboard.php');

This may be a single space or any error. Also try to remove any blank space / balank lines before using header('location: user/dashboard.php');

If you need more help, Please let me know.
Thanks,
Ajay

Not on topic, but carefull with this also.

foreach ($user as $A_key => $A_value) {
    foreach ($value as $B_key => $B_value) {
        $_SESSION[$A_key] = $A_value;
    }                           
}

What I called $A_key and $A_value are accessible within the second loop... that might mess things up for you someday.

Thank you all Guys but the problem is changed !!!

1- first i tried this code at first ob_start(); and the error message disappear ??!! but this is not the end i got then this error >>

Warning: Invalid argument supplied for foreach() in /home/user/dashboard.php on line 39

now what ??!!

Usually that last error means that you're trying to loop through something other than an array. Try:

if(is_array($expectedArray)){
    foreach(....){
        //your code
    }
} else {
    //take care of the non-array variable
}

Keep in mind my previous post, since you might get that error in the sub-loop as well. Don't forget to echo or var_dump the objects you're putting into the foreach to make sure what they are.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.