Results 1 to 8 of 8

Thread: php placement question

  1. #1
    Member DaveGears86's Avatar
    Join Date
    Oct 2011
    Location
    Blackheath, London
    Posts
    44

    php placement question

    I have created a log-in area and everything is technically sound on the front of logging in, viewing secure pages and then logging out.

    At the very top of my secure pages I have the following PHP:

    PHP Code:
    <?php

    session_start
    ();

    if (
    $_SESSION['username'])
    {
        
        echo 
    "You are logged in as: ".$_SESSION['username'];
        echo 
    "<p>";
        echo 
    "<a href='logout.php'>Click here to logout</a>";
        
    }
    else
        
    header ("location: index2.htm");

    ?>
    <html>
    <title>blah blah blah</title>
    This displays "You are logged in as: MrPink" with "Click here to logout" at the top center of the page (obviously very undesirable as it pushes the content/divs down the page). So, could somebody help explain to me how I can place the PHP "You are logged in as : MrPink" where ever I want on the page?

    (and yep, Reservoir Dogs fan)

    Thanks

  2. #2
    Moderator Corrosive's Avatar
    Join Date
    May 2010
    Location
    Bristol, UK
    Posts
    836
    Just break up the PHP into;

    PHP Code:
    <?php

    session_start
    ();

    ?>
    at the top of the page.

    Then place this part where you want the message to appear (in a div I'd imagine);

    PHP Code:

    <?php

    if ($_SESSION['username'])
    {
        
        echo 
    "You are logged in as: ".$_SESSION['username'];
        echo 
    "<p>";
        echo 
    "<a href='logout.php'>Click here to logout</a>";
        
    }
    else
        
    header ("location: index2.htm");

    ?>
    PHP can run and output anywhere you call it on the page.

  3. #3
    Member DaveGears86's Avatar
    Join Date
    Oct 2011
    Location
    Blackheath, London
    Posts
    44
    Thats brilliant and yet so simple, Thanks Corrosive.

    I do have a problem though - Designs By Maverickā„¢ - Web Design by David Barkham

    Warning: Cannot modify header information - headers already sent by (output started at /home/designs6/public_html/membersarea.php:13) in /home/designs6/public_html/membersarea.php on line 205
    and line 205 is -
    header ("location: index2.htm");
    Any ideas what's wrong here?
    Thanks

  4. #4
    Member DaveGears86's Avatar
    Join Date
    Oct 2011
    Location
    Blackheath, London
    Posts
    44
    ah ha! I managed to sort it by adding :

    This to the top of the entire document:
    PHP Code:
    <?php 

    ob_start
    (); 

    ?>
    and this at the end of the entire document:
    PHP Code:
    <?php 

    ob_end_flush
    (); 

    ?>
    simples, thanks.

  5. #5
    Moderator Corrosive's Avatar
    Join Date
    May 2010
    Location
    Bristol, UK
    Posts
    836
    You could have disabled the warning using error handling but your solution is fine. Remember that error handling should be at a higher level during development and then it can be suppressed in the production environment

  6. #6
    Member DaveGears86's Avatar
    Join Date
    Oct 2011
    Location
    Blackheath, London
    Posts
    44
    ah ok,

    could you show me what you mean (what code would I add and where etc)?

    Thanks

  7. #7
    Moderator Corrosive's Avatar
    Join Date
    May 2010
    Location
    Bristol, UK
    Posts
    836
    This is from the PHP manual website;

    PHP Code:
    <?php

    // Turn off all error reporting
    error_reporting(0);

    // Report simple running errors
    error_reporting(E_ERROR E_WARNING E_PARSE);

    // Reporting E_NOTICE can be good too (to report uninitialized
    // variables or catch variable name misspellings ...)
    error_reporting(E_ERROR E_WARNING E_PARSE E_NOTICE);

    // Report all errors except E_NOTICE
    // This is the default value set in php.ini
    error_reporting(E_ALL E_NOTICE);

    // Report all PHP errors (see changelog)
    error_reporting(E_ALL);

    // Report all PHP errors
    error_reporting(-1);

    // Same as error_reporting(E_ALL);
    ini_set('error_reporting'E_ALL);

    ?>
    and shows your options for error handling.

    What you had was a warning only and should not have affected your application. Error handling is a debug tool really.

    BTW, here is your new bible; PHP: Hypertext Preprocessor horrendous website to view and navigate but soooo many useful things it worth persevering with it

  8. #8
    Member DaveGears86's Avatar
    Join Date
    Oct 2011
    Location
    Blackheath, London
    Posts
    44
    Brilliant, thanks I appreciate it.

Posting Permissions

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