setcookie or session_register

Discussion in 'PHP' started by asmon, Jun 15, 2007.

  1. #1
    I have seen 2 guides about how to create a login page.
    one using setcookie and another user session_register.
    is there a difference between the two?


    and i have tried using one of the ways but i get an error when trying to
    connect. (default.php is the page where i am trying to loging, the
    menu.php is the login script which i include in every page)
    line 10 just starts a php code, line 156,157 are setting cookies and line 160 is a header to the default page if login worked.
    if i remove the last header, i still get the first 2 errors.

    "Warning: Cannot modify header information - headers already sent by (output started at C:\webs\Apache\Game\default.php:10) in C:\webs\Apache\Game\Menu.php on line 156

    Warning: Cannot modify header information - headers already sent by (output started at C:\webs\Apache\Game\default.php:10) in C:\webs\Apache\Game\Menu.php on line 157

    Warning: Cannot modify header information - headers already sent by (output started at C:\webs\Apache\Game\default.php:10) in C:\webs\Apache\Game\Menu.php on line 160"
     
    asmon, Jun 15, 2007 IP
  2. krt

    krt Well-Known Member

    Messages:
    829
    Likes Received:
    38
    Best Answers:
    0
    Trophy Points:
    120
    #2
    Cookies are stored on the user's computer, can be modified by the user and are generally less secure. Sessions are stored on the server but expire once the user is no longer using the site. Also, use session variables, $_SESSION['var'] instead of the deprecated session_register()

    As for the errors, cookies (and other headers) cannot be sent after any output. So the cookie must be sent before any HTML or whatever other output you may have. Of course, the error message itself is output so once there is one of these errors, it will trigger a chain, so that is why you get a few or why one goes after you remove the header redirect.
     
    krt, Jun 15, 2007 IP
  3. asmon

    asmon Member

    Messages:
    56
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #3
    Thanks for your help.
    Didn't know i can't even use include to include a few variables if i want to
    send cookies.

    I have another question, how can i configure apache so everyone could excess the website nor just using localhost?
     
    asmon, Jun 16, 2007 IP
  4. TwistMyArm

    TwistMyArm Peon

    Messages:
    931
    Likes Received:
    44
    Best Answers:
    0
    Trophy Points:
    0
    #4
    asmon: of course you can use include if you want to send cookies. You just have to make sure that those included files don't send output. 9 times out of 10 if there's an error like that and your include files are not setup to echo anything, it's normally whitespace outside of the <?php / ?> tags.

    Generally, you'll find that you've put a newline after the closing tag (?>) and that is what is causing the problem. Including by itself is not a problem for cookies.
     
    TwistMyArm, Jun 16, 2007 IP
  5. InFloW

    InFloW Peon

    Messages:
    1,488
    Likes Received:
    39
    Best Answers:
    0
    Trophy Points:
    0
    #5
    To answer this question find where the server mentions Listen. Add the following

    
    Listen 80
    
    Code (markup):
    This will tell it to listen on port 80 I imagine your server is set to only listen on port 80 of 127.0.0.1
     
    InFloW, Jun 16, 2007 IP
  6. asmon

    asmon Member

    Messages:
    56
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #6
    actually i have nothing else but "Listen 80"

    when trying to access the webpage

    "You are not authorized to view this page
    You might not have permission to view this directory or page using the credentials you supplied. "
     
    asmon, Jun 16, 2007 IP