SitePoint Sponsor

User Tag List

Results 1 to 7 of 7

Thread: require_once()?

  1. #1
    SitePoint Wizard lukeurtnowski's Avatar
    Join Date
    Mar 2003
    Location
    Coronado
    Posts
    1,618
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)

    require_once()?

    I have a question about this function.
    I'm using a MySQL database so I created a file named db_conn.php so it can be called wherever needed.
    I have...
    PHP Code:
    <?
    if($_POST['name'] != "") {
    require_once(
    "db_conn.php");

    session_start();
    $user $_POST['name'];
    $pass $_POST['pass'];
    $result mysql_query("SELECT * FROM users WHERE name = '$user' AND password = '$pass'") or die(mysql_error()); 
    $row mysql_fetch_array$result );
    }
    ?>

    <span id="navbar">
    <?php
    if($row) {
    $_SESSION['id'] = $row['id'];
    header("Location: editable.php");
    exit;
    } else {
    ?>
    I was having issues when the db_conn.php page had no opening and closing PHP tags, but once I added them, the page worked good, why did I need those tags?
    "Oh, and Jenkins--apparently your mother died this morning."

  2. #2
    SitePoint Wizard bronze trophy chris.upjohn's Avatar
    Join Date
    Apr 2010
    Location
    Melbourne, AU
    Posts
    2,189
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    No matter what you need an opening PHP tag in every *.php file, think of the tag as been an identifier for that file. If we include the file into our script without an opening PHP tag the mime type would be type/text allowing the dump to just output to the page, if we add that opening tag the mime type then changes to type/application-php.

    Personally i don't recommend using PHP short tags (<?) as to me it doesn't represent PHP where as (<?php) makes more sense and is not mistaken as something else, also i have seen a lot of time in the past where the short tag can easily be mistaken for an XML tag which is the main reason i don't like them.

  3. #3
    SitePoint Enthusiast
    Join Date
    Aug 2010
    Location
    Perundurai, Tamilnadu, India
    Posts
    72
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by lukeurtnowski View Post
    I was having issues when the db_conn.php page had no opening and closing PHP tags, but once I added them, the page worked good, why did I need those tags?
    If you don't specify those tags, then it becomes normal text content.
    Normal text content cannot be interpreted by PHP, that's way your getting the error.

  4. #4
    Non-Member bronze trophy
    Join Date
    Nov 2009
    Location
    Keene, NH
    Posts
    3,760
    Mentioned
    23 Post(s)
    Tagged
    0 Thread(s)
    It is an annoying part of PHP -- a piece of legacy I REALLY think should be removed from the specification ENTIRELY... at least then it would stop people from opening and closing php on every blasted line turning their code into a needlessly complex train wreck.

    Though to put that in perspective, I say the same thing about how people use double quotes for EVERYTHING.

  5. #5
    SitePoint Enthusiast praveenkv1988's Avatar
    Join Date
    May 2011
    Posts
    40
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    All php files that will be executed as separate files need those starting and endings. When you use require_once, the functions and everything included there are loaded in to memory and its not copying the code to the new file.

  6. #6
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,788
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    You don't need the end tag when it comes at the very end of the file.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  7. #7
    SitePoint Enthusiast
    Join Date
    Sep 2009
    Posts
    53
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You should also have a look at why PHP short open tags are bad.


Bookmarks

Posting Permissions

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