Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New Coder
    Join Date
    Mar 2004
    Posts
    92
    Thanks
    0
    Thanked 0 Times in 0 Posts

    quick question..

    This is not working like I would like it too but I'm thinking it's not possible to do it this way... am I right?

    <html>
    <head>
    <title>
    </title>
    </head>
    <body>
    PHP Code:
    <?
        session_start
    ();
        if ((
    $_SESSION['userstatus'] != 'admin')) {
            
    header ("Location: /login.php");
        }
    ?>
    </body>
    </html>

  • #2
    New Coder
    Join Date
    Feb 2004
    Location
    California
    Posts
    96
    Thanks
    0
    Thanked 0 Times in 0 Posts
    session_start has to go on the VERY FIRST LINE. That includes before DOCTYPE and all.
    PHP Code:
    session_start();
    <html>
    <head>
    <title>
    </title>
    </head>
    <body>

    <?
        
    if (($_SESSION['userstatus'] != 'admin')) {
            
    header ("Location: /login.php");
        }
    ?>

    </body>
    </html>

  • #3
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,073
    Thanks
    11
    Thanked 98 Times in 96 Posts
    Also please keep your thread titles as decriptive as possible 'quick question' is bad juju
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  • #4
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    2 problems with Steveo31 post:
    - the session_start() should be enclosed with PHP tags
    - the redirect also needs to be placed before any output is sent to the client.

    So your code would be
    PHP Code:
    <?php
    session_start
    (); 
    if (
    $_SESSION['userstatus'] != 'admin') { 
         
    header ("Location: /login.php"); 
         die();
    }
    ?> 
    <html> 
    <head> 
    <title> 
    </title> 
    </head> 
    <body> 
    </body> 
    </html>
    (so also don't use shorttags and place a die() after the redirect, just to e safe)
    Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html

  • #5
    New Coder
    Join Date
    Mar 2004
    Posts
    92
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by raf
    2 problems with Steveo31 post:
    - the session_start() should be enclosed with PHP tags
    - the redirect also needs to be placed before any output is sent to the client.

    So your code would be
    PHP Code:
    <?php
    session_start
    (); 
    if (
    $_SESSION['userstatus'] != 'admin') { 
         
    header ("Location: /login.php"); 
         die();
    }
    ?> 
    <html> 
    <head> 
    <title> 
    </title> 
    </head> 
    <body> 
    </body> 
    </html>
    (so also don't use shorttags and place a die() after the redirect, just to e safe)
    I've read that some browsers don't really like: header ("Location: "); so i tried this:
    PHP Code:
    header("Location: http://".$_SERVER['HTTP_HOST'].dirname($_SERVER['PHP_SELF'])."/index.php"); 
    I keep getting: No input file specified. printed out on the page.. I have no clue why but i'm getting further then i did before...

    $_SERVER['HTTP_HOST'] should be pointing to www.maxforcepc.com/ and $_SERVER['PHP_SELF'] should be pointing to edit/ because that's where the script is located...

    Well, the header is working but it's forwarding the page to: http://www.maxforcepc.com/login.php instead of edit/login.php
    -Thanks,
    Rich
    Last edited by DiarYofaMadmaN; 05-03-2004 at 04:58 PM.

  • #6
    New Coder
    Join Date
    Mar 2004
    Posts
    92
    Thanks
    0
    Thanked 0 Times in 0 Posts
    nvm i fixed it i just had to use index.php instead of /index.php it was pointing to the wrong directory...

    -peace

  • #7
    New Coder
    Join Date
    Mar 2004
    Posts
    92
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Scratch what i said cause it's not working

    i'm using:

    PHP Code:
    header("Location: http://".$_SERVER['HTTP_HOST'].dirname($_SERVER['PHP_SELF'])."index.php"); 
    which I think should be forwarding to edit/index.php if my syntax is correct but it's not forwarding. It's forwarding to http://www.maxforcepc.com/editindex.php instead of edit/index.php. I tried putting a / infront of index.php but it's still coming back as editindex.php. I have no clue can someone help me with this problem? I'm sry to be bugging you guys but i'm getting really far with this and I'v been coding stuff on my own just can't figure out this problem even though i'm reading the php manual. I'm not trying to have you guys write the code for me...

    -peace

  • #8
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Just use
    PHP Code:
    header('Location: http://'.$_SERVER['HTTP_HOST']
                            .
    dirname($_SERVER['PHP_SELF'])
                            .
    '/index.php'); 
    Should work perfectly. If the / doesn't show, then make sure you saved the file, make sure you don't load a buffered file etc
    Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •