Print Reseller Scheme
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

php placement question

Discussion in 'Website Coding & Programming Forum:' started by DaveGears86, Jan 26, 2012.

  1. DaveGears86

    DaveGears86 Member

    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:
    <?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. Corrosive

    Corrosive Moderator Staff Member

    Just break up the PHP into;

    PHP:
    <?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:

    <?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. DaveGears86

    DaveGears86 Member

    Thats brilliant and yet so simple, Thanks Corrosive.

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

    and line 205 is -
    Any ideas what's wrong here?
    Thanks
     
  4. DaveGears86

    DaveGears86 Member

    ah ha! I managed to sort it by adding :

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

    ob_start
    (); 

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

    ob_end_flush
    (); 

    ?>
    simples, thanks.
     
  5. Corrosive

    Corrosive Moderator Staff Member

    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 :icon_biggrin:
     
  6. DaveGears86

    DaveGears86 Member

    ah ok,

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

    Thanks
     
  7. Corrosive

    Corrosive Moderator Staff Member

    This is from the PHP manual website;

    PHP:
    <?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 :icon_smile:
     
  8. DaveGears86

    DaveGears86 Member

    Brilliant, thanks I appreciate it.
     

Share This Page