Jump to content

How to run php in a html file?


usman07

Recommended Posts

i am new to php, but i have written a php code for a login, basically i have my index html page separate, so when i put it on the server, when i press log in, it takes me to another blank page, with written either saying your logged in, or incorrect password, so this proves my php code works.

 

But what i want is the writing to be displayed on my html file.

 

Could I run the php code on the html file with the two files being separate? or do i have to insert the php code in the html file? or other way around? (but when i do this, it ruins my html file and things below just disappear.

 

 

What I already know:

 

that I have to edit the .htacess file and include a line of code

 

would appreciate ur help, thank you.

 

 

Link to comment
Share on other sites

Thank you both for your replies, I will now insert my codes to gives you a better idea.

 

PHP code:

 

<?php

 

$username = $_POST["username"];

$password = $_POST["password"];

 

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!");

 

?>

 

 

HTML code (only the login,the whole html code is really long)

 

 

<div id="username">

<form action="login.php" 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

depending on your server setup you may not need to touch your htaccess. most servers will default to .php (if index.php exists)

I think what you are looking to do is get the user to try and log in, if nsuccessful show them the  sercure page, if not then redirect with a message. One way is using the 'get' parameter in php. Example:

change:

die('That user does not exist');

to

header('Location:index.php?error=notexist');

and in your index.php:

<div id="username">
   <form action="login.php" 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>
      <?php if($_GET['error']=='notexist'){
       <p>Sorry, That username does not exist</p>
       }
?>
      <p class="register">Not yet a member? <a href="Form.html">Register Here</a>, its Free!</p>
</div>

now please be aware that this is a very basic example and by no means complete, but should give you an idea

Link to comment
Share on other sites

Here, put all of this in the index.html page then RENAME index.html to index.php and upload it. :)

 

 


<?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);

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>');

?>

 

 

 

 

 

 

Link to comment
Share on other sites

Thank you both for your replies, I will now insert my codes to gives you a better idea.

 

PHP code:

 

<?php

 

$username = $_POST["username"];

$password = $_POST["password"];

 

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!");

 

?>

 

 

HTML code (only the login,the whole html code is really long)

 

 

<div id="username">

<form action="login.php" 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>

which page is this?

 

"AddType application/x-httpd-php .html"

 

is that the correct code to insert in the htaccess file?

depends, is your server using php as apache module or cgi?

 

iv renamed the file,but the php still opens up in a new page,rather than on the html page.

what do you mean, "opens in a new page"? be more specific.

 

Link to comment
Share on other sites

The error:

 

 

PHP Error Message

 

Parse error: syntax error, unexpected $end in /home/a3073051/public_html/index.php on line 167

 

Free Web Hosting

 

I see why, try this instead. -

 


<?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);

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>');
}

?>

Link to comment
Share on other sites

ok the page now shows up but when i try to log in: this error comes up:

 

 

PHP Error Message

 

Warning: mysql_connect() [function.mysql-connect]: Access denied for user 'retracted'@'10.1.1.39' (using password: YES) in /home/a3073051/public_html/index.php on line 59

 

Free Web Hosting

Couldn't connect!

Link to comment
Share on other sites

ok the page now shows up but when i try to log in: this error comes up:

 

 

PHP Error Message

 

Warning: mysql_connect() [function.mysql-connect]: Access denied for user 'retracted'@'10.1.1.39' (using password: YES) in /home/a3073051/public_html/index.php on line 59

 

Free Web Hosting

Couldn't connect!

 

You need to put in your database details again, I removed them in the code for your privacy.

Link to comment
Share on other sites

ok the page now shows up but when i try to log in: this error comes up:

 

 

PHP Error Message

 

Warning: mysql_connect() [function.mysql-connect]: Access denied for user 'retracted'@'10.1.1.39' (using password: YES) in /home/a3073051/public_html/index.php on line 59

 

Free Web Hosting

Couldn't connect!

then your mysql credentials are not correct.

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.