Jump to content

How to run php in a html file?


usman07

Recommended Posts

<code>

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"

"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

 

<head>

<meta http-equiv="content-type" content="text/html;charset=utf-8" />

<title>Home</title>

<link rel="stylesheet" href="imgs/style.css"/>

 

</head>

 

<body>

<!--Main Div Tag-->

<div id="wrapper">

 

<div id="header">

<div id="logo"><img src="imgs/Logo/Logo.png" alt=""/>

</div>

 

<div id="Searchbar">

 

<div id="box1"><input type="text" size="30" name="name" style="background-color:transparent;" /></div>

</div>

 

 

 

<div id="magnify">

<a href=""/><img src="imgs/Log In/magnify.png" alt=""/></div>

 

</div>

 

<div>

<a id="Home" href="index.html" title="Home"><span>Home</span></a>

<a id="Photoshop" href="Photoshop.html" title="Photoshop"><span>Photoshop</span></a>

<a id="Illustrator" href="Illustrator.html" title="illustrator"><span>illustrator</span></a>

<a id="Tips" href="Tips.html" title="Tips"><span>Tips</span></a>

<a id="AboutUs" href="About Us.html" title="AboutUs"><span>About Us</span></a>

 

</div>

 

<div id="main">

 

<div id="header1"><img src="imgs/Homepage tuts/Homepage Main/Newtuts.png" alt=""/></div>

 

<div id="header2"><img src="imgs/Log In/login.png" alt=""/>

 

 

<?php

 

$username = $_POST["username"];

$password = $_POST["password"];

//This if statement asks if the $username variable is set. If it is it executes the php script. Otherwise it echoes the login form.

if(isset($username)){

 

if ($username&&$password)

 

{

 

$connect = mysql_connect("mysql13.000webhost.com","a3073051_shakoor","usman1") or die("Couldn't connect!");

mysql_select_db("a3073051_login") or die("Couldn't find db");

 

$query = mysql_query("SELECT * FROM users WHERE username='$username'");

 

$numrows = mysql_num_rows($query);

 

if ($numrows!=0)

 

{

 

while ($row = mysql_fetch_assoc($query))

 

{

  $dbusername = $row['username'];

  $dbpassword = $row['password'];

}

 

//check to see if they match

if ($username==$dbusername&&$password==$dbpassword)

 

{

echo "You're in!";

}

else

  echo "incorrect password!";

 

}

else

  die("That user does not exist!");

 

}

else

  die("Please enter a username and password!");

}

//This next bit echoes the login form unless the $username variable is set.

else {

echo ('<div id="username">

  <form action="" method="post"/>

   

      <table><tr><td>

      <img src="imgs/Log In/username.png" alt=""/>

      </td><td>

      <input type="text" size="30" name="username" style="background-color:transparent;" />

      </td></tr></table>

     

      <table><tr><td>

      <img src="imgs/Log In/password.png" alt=""/>

      </td><td>

      <input type="password" name="password" size="30" />

      </td></tr></table>

     

      <form id="submitb" action="">

      <input type="submit" value="Log in" />

      </form>

     

      <p class="register">Not yet a member? <a href="Form.html">Register Here</a>, its Free!</p>

</div>');

}

 

?>

 

 

</div>

 

<div id="table1">

<table><tr><td>

  <a href="imgs/Homepage tuts/Create a HD Television.html"><img src="imgs/Homepage tuts/Homepage Main/Homepage Tutorials/TVtut/icon1.png" alt=""/></a>

</td><td>

<a href="imgs/Homepage tuts/tut2.html"><img src="imgs/Homepage tuts/Homepage Main/Homepage Tutorials/tut2/icontut2.png" alt=""/></a>

</td></tr></table>

 

<table><tr><td>

  <a href="imgs/Homepage tuts/tut3.html"><img src="imgs/Homepage tuts/Homepage Main/Homepage Tutorials/tut3/tut3icon.png" alt=""/></a>

</td><td>

  <a href="imgs/Homepage tuts/tut4.html"><img src="imgs/Homepage tuts/Homepage Main/Homepage Tutorials/tut4/tut4icon.png" alt=""/></a>

</td></tr></table>

 

<table><tr><td>

<a href="imgs/Homepage tuts/tut5.html"><img src="imgs/Homepage tuts/Homepage Main/Homepage Tutorials/tut5/tut5icon.png" alt=""/></a>

</td><td>

  <a href="imgs/Homepage tuts/tut6.html"><img src="imgs/Homepage tuts/Homepage Main/Homepage Tutorials/tut6/tut6icon.png" alt=""/></a>

</td></tr></table>

</div>

 

</div>

 

<div id="footer">

 

<div id="footernav">

<a id="fHome" href="index.html" title="Home"><span>Home</span></a>

<a id="fPhotoshop" href="Photoshop.html" title="Photoshop"><span>Photoshop</span></a>

<a id="fillustrator" href="Illustrator.html" title="illustrator"><span>illustrator</span></a>

<a id="fTips" href="Tips.html" title="Tips"><span>Tips</span></a>

<a id="fAboutUs" href="About Us.html" title="AboutUs"><span>About Us</span></a>

 

</div>

 

 

 

 

</div>

 

</div>

 

 

 

 

</body>

</html>

</code>

Link to comment
Share on other sites

Here you go, this should work. Just replace all of the previous php with this and put your password and username for the database back in:

 

<?php

 

$username = $_POST["username"];

$password = $_POST["password"];

//This if statement asks if the $username variable is set. If it is it executes the php script. Otherwise it echoes the login form.

if(isset($username)){

 

if (!($username == " " && $password == " "))

 

{

 

$connect = mysql_connect("mysql13.000webhost.com","retracted","retracted") or die("Couldn't connect!");

mysql_select_db("a3073051_login") or die("Couldn't find db");

 

$query = mysql_query("SELECT * FROM users WHERE username='$username'");

 

$numrows = mysql_num_rows($query);

 

  $dbusername = $row['username'];

  $dbpassword = $row['password'];

}

else {

echo ('<div id="username">

  <form action="" method="post"/>

<font face="Lucinda" color="red"> Please supply a username and password </font>

      <table><tr><td>

      <img src="imgs/Log In/username.png" alt=""/>

      </td><td>

      <input type="text" size="30" name="username" style="background-color:transparent;" />

      </td></tr></table>

 

      <table><tr><td>

      <img src="imgs/Log In/password.png" alt=""/>

      </td><td>

      <input type="password" name="password" size="30" />

      </td></tr></table>

 

      <form id="submitb" action="">

      <input type="submit" value="Log in" />

      </form>

 

      <p class="register">Not yet a member? <a href="Form.html">Register Here</a>, its Free!</p>

</div>');

}

//check to see if they match

if ($username == $dbusername && $password == $dbpassword) {

echo "You're in!";

}

else

  echo ('<div id="username">

  <form action="" method="post"/>

<font face="Lucinda" color="red"> Wrong username or password, please try again </font>

      <table><tr><td>

      <img src="imgs/Log In/username.png" alt=""/>

      </td><td>

      <input type="text" size="30" name="username" style="background-color:transparent;" />

      </td></tr></table>

 

      <table><tr><td>

      <img src="imgs/Log In/password.png" alt=""/>

      </td><td>

      <input type="password" name="password" size="30" />

      </td></tr></table>

 

      <form id="submitb" action="">

      <input type="submit" value="Log in" />

      </form>

 

      <p class="register">Not yet a member? <a href="Form.html">Register Here</a>, its Free!</p>

</div>');

 

}

 

//This next bit echoes the login form unless the $username variable is set.

else {

echo ('<div id="username">

  <form action="" method="post"/>

   

      <table><tr><td>

      <img src="imgs/Log In/username.png" alt=""/>

      </td><td>

      <input type="text" size="30" name="username" style="background-color:transparent;" />

      </td></tr></table>

     

      <table><tr><td>

      <img src="imgs/Log In/password.png" alt=""/>

      </td><td>

      <input type="password" name="password" size="30" />

      </td></tr></table>

     

      <form id="submitb" action="">

      <input type="submit" value="Log in" />

      </form>

     

      <p class="register">Not yet a member? <a href="Form.html">Register Here</a>, its Free!</p>

</div>');

}

 

?>

Link to comment
Share on other sites

ok i did that, now the text 'Wrong username or password, please try again' is displayed at all times, and if i do type in the correct username and password,nothing happens and the text ,Wrong username or password, please try again,is still displayed

Link to comment
Share on other sites

ok i did that, now the text 'Wrong username or password, please try again' is displayed at all times, and if i do type in the correct username and password,nothing happens and the text ,Wrong username or password, please try again,is still displayed

 

What is the web link so I can see what is happening?

Link to comment
Share on other sites

Right, I figured it out. DO the same thing as last time. Here =

 


<?php

$username = $_POST["username"];
$password = $_POST["password"];
//This if statement asks if the $username variable is set. If it is it executes the php script. Otherwise it echoes the login form.
if(isset($username)){

if (!($username == " " && $password == " "))

{

$connect = mysql_connect("mysql13.000webhost.com","retracted","retracted") or die("Couldn't connect!");
mysql_select_db("a3073051_login") or die("Couldn't find db");

$query = mysql_query("SELECT * FROM users WHERE username='$username'");

$row = mysql_fetch_array($query);

$numrows = mysql_num_rows($query);

   $dbusername = $row['username']; 
   $dbpassword = $row['password'];
}
else {
echo ('<div id="username">
   <form action="" method="post"/>
	<font face="Lucinda" color="red"> Please supply a username and password </font>
      <table><tr><td>
      <img src="imgs/Log In/username.png" alt=""/>
      </td><td>
      <input type="text" size="30" name="username" style="background-color:transparent;" />
      </td></tr></table>

      <table><tr><td>
      <img src="imgs/Log In/password.png" alt=""/>
      </td><td>
      <input type="password" name="password" size="30" />
      </td></tr></table>

      <form id="submitb" action="">
      <input type="submit" value="Log in" />
      </form>

      <p class="register">Not yet a member? <a href="Form.html">Register Here</a>, its Free!</p>
</div>');
}
//check to see if they match
if ($username == $dbusername && $password == $dbpassword) {
echo "You're in!";
}
else
  	echo ('<div id="username">
   <form action="" method="post"/>
	<font face="Lucinda" color="red"> Wrong username or password, please try again </font>
      <table><tr><td>
      <img src="imgs/Log In/username.png" alt=""/>
      </td><td>
      <input type="text" size="30" name="username" style="background-color:transparent;" />
      </td></tr></table>

      <table><tr><td>
      <img src="imgs/Log In/password.png" alt=""/>
      </td><td>
      <input type="password" name="password" size="30" />
      </td></tr></table>

      <form id="submitb" action="">
      <input type="submit" value="Log in" />
      </form>

      <p class="register">Not yet a member? <a href="Form.html">Register Here</a>, its Free!</p>
</div>');

}

//This next bit echoes the login form unless the $username variable is set.
else {
echo ('<div id="username">
   <form action="" method="post"/>
    
      <table><tr><td>
      <img src="imgs/Log In/username.png" alt=""/>
      </td><td>
      <input type="text" size="30" name="username" style="background-color:transparent;" />
      </td></tr></table>
      
      <table><tr><td>
      <img src="imgs/Log In/password.png" alt=""/>
      </td><td>
      <input type="password" name="password" size="30" />
      </td></tr></table>
      
      <form id="submitb" action="">
      <input type="submit" value="Log in" />
      </form>
      
      <p class="register">Not yet a member? <a href="Form.html">Register Here</a>, its Free!</p>
</div>');
}

?>

 

The problem was that it wasn't actually fetching an array ($row = mysql_fetch_array($query);) so I added that in and it should work.

Link to comment
Share on other sites

Don't worry, it's fine. Okay, I've double and triple checked this -

 

<?php

 

$user = $_REQUEST['username'];

$pass = $_REQUEST['password'];

$subm = $_REQUEST['submit'];

 

if(empty($user) || empty($pass)) {

echo ('<div id="username">

  <form action="" method="post"/>

<font face="Lucinda" color="red"> Please supply a username and password. </font>

      <table><tr><td>

      <img src="imgs/Log In/username.png" alt=""/>

      </td><td>

      <input type="text" size="30" name="username" style="background-color:transparent;" />

      </td></tr></table>

 

      <table><tr><td>

      <img src="imgs/Log In/password.png" alt=""/>

      </td><td>

      <input type="password" name="password" size="30" />

      </td></tr></table>

 

      <form id="submitb" action="">

      <input type="submit" value="Log in" />

      </form>

 

      <p class="register">Not yet a member? <a href="Form.html">Register Here</a>, its Free!</p>

</div>');

 

}

else {

if(isset($subm)){

 

$myServer = "mysql_server"; 

$myUser = "username";

$myPass = "password";

$myDB = "database_name";

 

//connection to the database

$dbhandle = mysql_connect($myServer, $myUser, $myPass)

  or die("Couldn't connect to SQL Server on $myServer");

 

//select a database to work with

$selected = mysql_select_db($myDB, $dbhandle)

  or die("Couldn't open database $myDB");

 

$query = "SELECT * FROM spotty WHERE username = '" . $user . "' AND password = '" . $pass ."'";

 

$result = mysql_query($query) or die('Error Getting user_info');

 

$row = mysql_fetch_array($result);

 

if($user == $row['username'] && $pass == $row['password']){

echo "You're in!";

}

else {

echo ('<div id="username">

  <form action="" method="post"/>

<font face="Lucinda" color="red"> Wrong username and password. </font>

      <table><tr><td>

      <img src="imgs/Log In/username.png" alt=""/>

      </td><td>

      <input type="text" size="30" name="username" style="background-color:transparent;" />

      </td></tr></table>

 

      <table><tr><td>

      <img src="imgs/Log In/password.png" alt=""/>

      </td><td>

      <input type="password" name="password" size="30" />

      </td></tr></table>

 

      <form id="submitb" action="">

      <input type="submit" value="Log in" />

      </form>

 

      <p class="register">Not yet a member? <a href="Form.html">Register Here</a>, its Free!</p>

</div>');

}

 

}

 

else {

echo ('<div id="username">

  <form action="" method="post"/>

      <table><tr><td>

      <img src="imgs/Log In/username.png" alt=""/>

      </td><td>

      <input type="text" size="30" name="username" style="background-color:transparent;" />

      </td></tr></table>

 

      <table><tr><td>

      <img src="imgs/Log In/password.png" alt=""/>

      </td><td>

      <input type="password" name="password" size="30" />

      </td></tr></table>

 

      <form id="submitb" action="">

      <input type="submit" id="submit" name="submit" value="Log in" />

      </form>

 

      <p class="register">Not yet a member? <a href="Form.html">Register Here</a>, its Free!</p>

</div>');

}

}

?>

Link to comment
Share on other sites

The current (previously posted) code will 'log you in' when nothing has been entered because an empty string is equal to an empty string and the code is not really checking if anything was entered. It's actually checking if anything other than a space was entered and an empty string is something other than a space.

 

The current (previously posted) logic contains many issues. You need to slow down and first define what you want to do, then write and test the code needed to perform each step that you have defined.

 

Edit: The latest code posted also contains coding issues, such as not repeating the form code three times that only differ in an error message being displayed. That makes a wall of code. What happens when you want to change the look or layout of the form? You have to find and make the change three times.

 

The form is also invalid (two <form  > tags) and doesn't have a field named 'submit', so the posted code still won't work.

 

Recommendation - slow down when defining, writing, and testing code.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.