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.
Page 1 of 2 12 LastLast
Results 1 to 15 of 17
  1. #1
    Regular Coder cineweekly.com's Avatar
    Join Date
    Aug 2010
    Posts
    485
    Thanks
    14
    Thanked 3 Times in 3 Posts

    How can I put PHP code into an HTML file being required?

    Sorry, I'm not sure of the terminology I'm very new to PHP and still learning.
    First of all, here is my html code:
    Code:
    <head>
    <?php require("head.html"); ?>
    </head>
    <body>
    <div class="link"><a href=" '.$link.' ">Random</a></div>
    </body>
    How can I insert the following code into the head.html file? Currently, the file only contains <?php echo ' Some html '; ?>
    PHP Code:
    // Predefinitions
    $links file('/articles.txt');
    $currentPage $_SERVER['PHP_SELF'];

    // make sure links are pure and no whitespaces are left
    foreach ( $links as &$link )
    {
        
    $link trim($link);
    }

    // prepare for history
    if ( ! array_key_exists('visited_links'$_SESSION) )
    {
        
    $_SESSION['visited_links'] = array();
    }
    // add current site to visited links if it wasnt visited earlier
    if ( ! in_array($currentPage$_SESSION['visited_links']) )
    {
        
    $_SESSION['visited_links'][] = $currentPage
    }

    // get all those links which were not visited yet kinda $links = $allLinks - $visitedLinks
    $potentialLinks array_diff($links$_SESSION['visited_links']);

    $randomLinkId mt_rand(0count($potentialLinks)-1);
    $randomLinkIds array_keys($potentialLinks);
    $randomLink $potentialLinks[$randomLinkIds[$randomLinkId]]; 

  • #2
    Regular Coder djh101's Avatar
    Join Date
    May 2009
    Location
    California
    Posts
    616
    Thanks
    48
    Thanked 65 Times in 65 Posts
    Just insert it? And, of course, change head.html to head.php.
    "Yeah science!"
    Online Science Tools

  • #3
    Regular Coder cineweekly.com's Avatar
    Join Date
    Aug 2010
    Posts
    485
    Thanks
    14
    Thanked 3 Times in 3 Posts
    Quote Originally Posted by djh101 View Post
    Just insert it? And, of course, change head.html to head.php.
    My html parses as php server-side.
    Where do I insert the php? I've tried this format:

    Code:
    <?php
    php code from original post
    
    echo '
    html
    ';
    ?>

    But I'm getting this error in my error log:

    PHP Warning: array_key_exists() [<a href='function.array-key-exists'>function.array-key-exists</a>]: The second argument should be either an array or an object

    This is the line in question:
    if ( ! array_key_exists('visited_links', $_SESSION) )

    The code works when the link is echoed from within the php (from earlier) so the problem has to be the location I'm inserting the php.
    Last edited by cineweekly.com; 01-20-2012 at 04:40 AM.

  • #4
    Regular Coder cineweekly.com's Avatar
    Join Date
    Aug 2010
    Posts
    485
    Thanks
    14
    Thanked 3 Times in 3 Posts
    Any idea on why I'm getting this error? It's only an error when inserting it through php. In a plain html doc there are no errors.

  • #5
    Senior Coder
    Join Date
    Jul 2011
    Posts
    1,226
    Thanks
    3
    Thanked 171 Times in 171 Posts
    If you're mixing logic and markup (html), which I don't recommend, then you can use ?> and <?php to stop parsing as PHP and treat as plain text. So,

    PHP Code:
    <?php
    // Some php code
    ?>
    html and plain text
    <?php
    // Some more PHP
    ?>
    Edit: The most likely explanation for your error is unescaped quotation marks - echo "<div style="stuff">" as an example.
    Useful function to retrieve difference in times
    The best PHP resource
    A good PHP FAQ
    PLEASE remember to wrap your code in [PHP] tags.
    PHP Code:
    // Replace this
    if(isset($_POST['submitButton']))
    // With this
    if(!empty($_POST))
    // Then check for values/forms. Some IE versions don't send the submit button 
    Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.

  • #6
    Regular Coder cineweekly.com's Avatar
    Join Date
    Aug 2010
    Posts
    485
    Thanks
    14
    Thanked 3 Times in 3 Posts
    I fit the php in properly as you said and tried every way to escape things but the problem persists. Same error message.

  • #7
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Session superglobal is constructed during session_start. You need to have this call within any script that uses it.

  • #8
    Regular Coder cineweekly.com's Avatar
    Join Date
    Aug 2010
    Posts
    485
    Thanks
    14
    Thanked 3 Times in 3 Posts
    Quote Originally Posted by Fou-Lu View Post
    Session superglobal is constructed during session_start. You need to have this call within any script that uses it.
    The code works completely on it's own, it only gives the error when I insert it in this manner. On my individual pages I have a php require calling for the code that makes up my sidebar. In that sidebar file I have a php echo then I close it and insert the code from above, close it, and then have another php echo with the rest of my code.

    As I said, when I have the code on the individual page rather than inside the required file, it works fine.

  • #9
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Do you mean the other way around? I can see this working fine when included into another script that does issue a session_start. Stand alone it will not work as $_SESSION is not constructed without the call to session_start. Since the cost to use overhead is tremendously low, issuing a session_start() on any script that uses $_SESSION makes sense.

  • #10
    Regular Coder cineweekly.com's Avatar
    Join Date
    Aug 2010
    Posts
    485
    Thanks
    14
    Thanked 3 Times in 3 Posts
    Since I'm super new to php could you guide me on how to fit the session code into the code I already have? After experimenting, I'm guessing it's more than simply putting session_start(); in the php.
    Last edited by cineweekly.com; 01-25-2012 at 05:44 AM.

  • #11
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    No, its not. All you need is session_start() at the top of your page, and $_SESSION will now exist and can be populated and read from.

  • Users who have thanked Fou-Lu for this post:

    cineweekly.com (01-26-2012)

  • #12
    Regular Coder cineweekly.com's Avatar
    Join Date
    Aug 2010
    Posts
    485
    Thanks
    14
    Thanked 3 Times in 3 Posts
    When I do that, both with and without the semi-colon, I get these errors:

    PHP Warning: session_start() [<a href='function.session-start'>function.session-start</a>]: Cannot send session cookie - headers already sent by (output started at /public_html/index.html:6) in /head.html on line 2

    PHP Warning: session_start() [<a href='function.session-start'>function.session-start</a>]: Cannot send session cache limiter - headers already sent (output started at /public_html/index.html:6) in /head.html on line 2

    Remember, the random link still works fine, these errors only appear in my error_log.
    Last edited by cineweekly.com; 01-25-2012 at 11:12 PM.

  • #13
    Senior Coder
    Join Date
    Aug 2006
    Posts
    1,234
    Thanks
    10
    Thanked 270 Times in 269 Posts
    You need the session_start at the very top, or at least before any output to the browser (ie, before DOCTYPE, or any of that stuff).

    Dave

  • Users who have thanked tracknut for this post:

    cineweekly.com (01-26-2012)

  • #14
    Regular Coder cineweekly.com's Avatar
    Join Date
    Aug 2010
    Posts
    485
    Thanks
    14
    Thanked 3 Times in 3 Posts
    Quote Originally Posted by tracknut View Post
    You need the session_start at the very top, or at least before any output to the browser (ie, before DOCTYPE, or any of that stuff).

    Dave
    Oh, wow, I think that did it, thanks guys. Now I need to find a way to easily insert it into all my pages. The topmost "php require" that I have is for content in the <head>

  • #15
    Senior Coder
    Join Date
    Aug 2006
    Posts
    1,234
    Thanks
    10
    Thanked 270 Times in 269 Posts
    What all do you have in your html file between this session_start and your included header? Can you not stick all of it inside the included header file, so the top of your code looks something like:
    Code:
    <?php include ('head.inc'); ?>
    
    <body>
    ...blah blah


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

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