Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

embedding php within html

Options
  • 19-02-2008 1:25pm
    #1
    Closed Accounts Posts: 51 ✭✭


    im building a website, using php for the functionality, such as login registration, search and contact us for example
    i have already done the php seperately in standalong php, and it works fine.
    no i want to get the same php code/functionality working within the css template im using.
    is this possible?

    here are the two pieces of code
    login.php
    login.html

    first the css template code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <!--
    Design by Free CSS Templates
    http://www.freecsstemplates.org
    Released for free under a Creative Commons Attribution 2.5 License
    
    Name       : Pluralism
    Description: A two-column, fixed-width template fit for 1024x768 screen resolutions.
    Version    : 1.0
    Released   : 20071218
    
    -->
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8" />
    <title>Search4Trade</title>
    <meta name="keywords" content="" />
    <meta name="description" content="" />
    <link href="style.css" rel="stylesheet" type="text/css" media="screen" />
    </head>
    <body>
    <div id="wrapper" style="width: 961px; height: 564px">
        <div id="wrapper2">
            <div id="header">
                <div id="logo">
                    <h1>search4trade.net</h1>
                </div>
                <div id="menu">
                    <ul>
                        <li><a href="#">Search </a></li>
                        <li><a href="#">Forum</a></li>
                        <li><a href="#">Register</a></li>
                        <li><a href="#">About Us</a></li>
                        <li><a href="#">Contact</a></li>
                    </ul>
                </div>
            </div>
            <div id="page">
                <div id="content" style="width: 766px; height: 205px">
                    <div class="post">
                        <h2 class="title"><a href="#">Welcome to Search4Trade</a></h2>
                        <div class="entry">
    &nbsp;</div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <p style="text-align: center">(c) 2008 Search4Trade.net</p>
    </body>
    
    
    </html>
    


    and now the php:
    <?php
    // Connects to your Database
    mysql_connect("localhost", "root", "password") or die(mysql_error());
    mysql_select_db("mydatabase") or die(mysql_error());
    
    //Checks if there is a login cookie
    if(isset($_COOKIE['ID_my_site']))
    
    //if there is, it logs you in and directes you to the members page
    {
    $username = $_COOKIE['username'];
    $pass = $_COOKIE['password'];
    $check = mysql_query("SELECT * FROM mytable WHERE username = '$username'")or die(mysql_error());
    while($info = mysql_fetch_array( $check ))
    {
    if ($pass != $info['password'])
    {
    }
    else
    {
    header("Location: submit.html");
    
    }
    }
    }
    
    //if the login form is submitted
    if (isset($_POST['submit'])) { // if form has been submitted
    
    // makes sure they filled it in
    if(!$_POST['username'] | !$_POST['pass']) {
    die('You did not fill in a required field.');
    }
    // checks it against the database
    
    
    $check = mysql_query("SELECT * FROM mytable WHERE username = '".$_POST['username']."'")or die(mysql_error());
    
    //Gives error if user dosen't exist
    $check2 = mysql_num_rows($check);
    if ($check2 == 0) {
    die('That user does not exist in our database.
    <ahref=register.php>Click Here to Register</a>');
    }
    while($info = mysql_fetch_array( $check ))
    {
    $_POST['pass'] = stripslashes($_POST['pass']);
    $info['password'] = stripslashes($info['password']);
    $_POST['pass'] = md5($_POST['pass']);
    
    //gives error if the password is wrong
    if ($_POST['pass'] != $info['password']) {
    die('Incorrect password, please try again.');
    }
    
    else
    {
    
    // if login is ok then we add a cookie
    $_POST['username'] = stripslashes($_POST['username']);
    $hour = time() + 3600;
    setcookie(ID_my_site, $_POST['username'], $hour);
    setcookie(Key_my_site, $_POST['pass'], $hour);
    
    //then redirect them to the members area
    include("search1.html");
    }
    }
    }
    else
    {
    
    // if they are not logged in
    ?>
    <form action="<?php echo $_SERVER['PHP_SELF']?>" method="post">
    <table border="0">
    <tr><td colspan=2><h1>Login</h1></td></tr>
    <tr><td>Username:</td><td>
    <input type="text" name="username" maxlength="40">
    </td></tr>
    <tr><td>Password:</td><td>
    <input type="password" name="pass" maxlength="50">
    </td></tr>
    <tr><td colspan="2" align="right">
    <input type="submit" name="submit" value="Login">
    </td></tr>
    </table>
    </form>
    <?php
    }
    
    ?>
    

    thanks for looking!


Comments

  • Closed Accounts Posts: 4,655 ✭✭✭Ph3n0m


    Yes you can

    However you need to rename login.html to something else - possible index.php

    And then you can use
    <?php include ('login.php'); ?>
    

    to include your login stuff into the index.php

    The most important thing is that login.html needs to be renamed with a .php extension otherwise the server wont know that php code has to be rendered on the page


Advertisement