开发者

It wont set the session?

I have this php login system, I had it working great on localhost, I bought a webhotel and now it doesn't work no longer, and i can't find where it goes wrong.

I get no errors.

The login page is in index.php, and when you sign in, and if everythings ok (no errors/wrong pw etc.) then you will be redirected to home.php.

This is not the case. When I log in, it just refreshes the index.php and outputs this at top:

Warning: Cannot modify header information - headers already sent by (httpd.www/oBz/index.php:2) in httpd.www/oBz/index.php on line 221

on line 221 there's: header("Location: home.php");

ok, so i went to home.php manually by enter in the address. Now in home.php I have this at top:

include 'dbc.php';
page_protect();
echo "HELLO WORLD";

page_protect checks if there's any sessions set or cookie(remember me), but if something has been set you will see the content "HELLO WORLD" else you wont.

But right now when i enter home.php I just receive this:

Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at 开发者_JAVA百科httpd.www/oBz/dbc.php:29) in httpd.www/oBz/dbc.php on line 69

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at httpd.www/oBz/dbc.php:29) in httpd.www/oBz/dbc.php on line 69

Warning: Cannot modify header information - headers already sent by (output started at httpd.www/oBz/dbc.php:29) in httpd.www/oBz/dbc.php on line 117

Line 69 theres session_start(), and it's the first line in the function page_protect();

line 117 theres header("Location: index.php") and is there to redirect if you are not logged in(session set/cookie set)

Hope i provided information enough, if not just comment what you need, and i'll try my best to provide it to you..

Thank you

Update:

Here is dbc.php: http://phpbin.net/x/999009567

**index.php where you log in and where the session sets http://phpbin.net/x/1564167411

**UPDATE: I now solved the header warning/errors but that was not the solution for the session issue!

**UPDATE: phpbin.net/x/25857430 the updated dbc.php, all the html that was in the dbc.php previously is in a new file top.php. I include the top.php file AFTER the doLogin function section in index.php, so there doesnt get any errors with the headers..

***UPDATE: The problem is somewhere here: http://phpbin.net/x/557713701 thats why its redirecting me to index.php all the time


You've already sent output to the browser with all that HTML that exists before your first bit of PHP.

You need to rearrange the code, so that anything which needs to send headers happens before any HTML is sent to the browser.

So:

<?php
session_start();

?>
<html>
<head>
...

The simplest answer may be to move all of that HTML after the PHP code. There's a session_regenerate_id() call in there and another session_start() in the logout function.

For that matter, why do have any HTML in this file anyway? Beyond the session functions, I spotted at least 2 header() calls.

Update:

if(isset($_COOKIE['user_id']) && isset($_COOKIE['user_key'])){
    /* we double check cookie expiry time against stored in database */

// I snipped a bunch of code, to point out what's going on here

  } else {
    header("Location: index.php");
    die();
    }

So, if the cookie values don't exist, what do you suppose happens here?


You're looking at the wrong bits of the error message. The part you're looking at is where it tries to send the headers (ie: where it realizes something is wrong). The relevant part, however, is the part where it began producing output. For the first error, it says the output was already started at index.php, line 2. You probably have whitespace in front of your opening <?php tag. I'd check that first...


And line 29 of dbc.php is echoing, or printing, or generating some output to the browser.... so what's the code in dbc.php around that line?

Likewise line 2 of index.php


I would guess :

Did you issue

die();

After header("Location: home.php");

???

You might just forget to close the output so that it prints out the rest of the page regardless of this redirection.

Also, please confirm that the encoding is good, should be UTF-8? Sometimes I got it wrong myself :(

Okay, updated answer:

266 header("Location: index.php");

303 header("Location: home.php");

340 header("Location: home.php");

You didn't issue Die() after these three lines. So this might be a risk :)

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜