usman07 Posted January 3, 2012 Share Posted January 3, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/254270-how-to-run-php-in-a-html-file/ Share on other sites More sharing options...
gristoi Posted January 3, 2012 Share Posted January 3, 2012 change your index.html to index.php Quote Link to comment https://forums.phpfreaks.com/topic/254270-how-to-run-php-in-a-html-file/#findComment-1303659 Share on other sites More sharing options...
Jacbey Posted January 3, 2012 Share Posted January 3, 2012 Ok, you probably already know you can't write php in a .html file. You can use iframes to insert a separate page but that probably wouldn't work. Can you provide your login code for both the html page and the php script? Quote Link to comment https://forums.phpfreaks.com/topic/254270-how-to-run-php-in-a-html-file/#findComment-1303661 Share on other sites More sharing options...
AyKay47 Posted January 3, 2012 Share Posted January 3, 2012 change your index.html to index.php yeah, the file extension needs to be .php for php code to work, the parser needs to know not to look for just html. Quote Link to comment https://forums.phpfreaks.com/topic/254270-how-to-run-php-in-a-html-file/#findComment-1303663 Share on other sites More sharing options...
usman07 Posted January 3, 2012 Author Share Posted January 3, 2012 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> Quote Link to comment https://forums.phpfreaks.com/topic/254270-how-to-run-php-in-a-html-file/#findComment-1303665 Share on other sites More sharing options...
usman07 Posted January 3, 2012 Author Share Posted January 3, 2012 So i rename the 'index.html' file to 'index.php', and include the neccessary code in the htaccess file. and thats it? and it would still work with the php file and index file being seperate but in the directory? Quote Link to comment https://forums.phpfreaks.com/topic/254270-how-to-run-php-in-a-html-file/#findComment-1303666 Share on other sites More sharing options...
usman07 Posted January 3, 2012 Author Share Posted January 3, 2012 "AddType application/x-httpd-php .html" is that the correct code to insert in the htaccess file? Quote Link to comment https://forums.phpfreaks.com/topic/254270-how-to-run-php-in-a-html-file/#findComment-1303667 Share on other sites More sharing options...
usman07 Posted January 3, 2012 Author Share Posted January 3, 2012 iv renamed the file,but the php still opens up in a new page,rather than on the html page. Quote Link to comment https://forums.phpfreaks.com/topic/254270-how-to-run-php-in-a-html-file/#findComment-1303668 Share on other sites More sharing options...
gristoi Posted January 3, 2012 Share Posted January 3, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/254270-how-to-run-php-in-a-html-file/#findComment-1303671 Share on other sites More sharing options...
Jacbey Posted January 3, 2012 Share Posted January 3, 2012 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>'); ?> Quote Link to comment https://forums.phpfreaks.com/topic/254270-how-to-run-php-in-a-html-file/#findComment-1303672 Share on other sites More sharing options...
AyKay47 Posted January 3, 2012 Share Posted January 3, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/254270-how-to-run-php-in-a-html-file/#findComment-1303673 Share on other sites More sharing options...
usman07 Posted January 3, 2012 Author Share Posted January 3, 2012 Jacbey I have tried your way, but I am getting a syntax error? Quote Link to comment https://forums.phpfreaks.com/topic/254270-how-to-run-php-in-a-html-file/#findComment-1303678 Share on other sites More sharing options...
Jacbey Posted January 3, 2012 Share Posted January 3, 2012 Jacbey I have tried your way, but I am getting a syntax error? What is the error? Quote Link to comment https://forums.phpfreaks.com/topic/254270-how-to-run-php-in-a-html-file/#findComment-1303682 Share on other sites More sharing options...
usman07 Posted January 3, 2012 Author Share Posted January 3, 2012 The error: PHP Error Message Parse error: syntax error, unexpected $end in /home/a3073051/public_html/index.php on line 167 Free Web Hosting Quote Link to comment https://forums.phpfreaks.com/topic/254270-how-to-run-php-in-a-html-file/#findComment-1303683 Share on other sites More sharing options...
AyKay47 Posted January 3, 2012 Share Posted January 3, 2012 The error: PHP Error Message Parse error: syntax error, unexpected $end in /home/a3073051/public_html/index.php on line 167 Free Web Hosting forgot closing bracket on last else condition. Quote Link to comment https://forums.phpfreaks.com/topic/254270-how-to-run-php-in-a-html-file/#findComment-1303687 Share on other sites More sharing options...
Jacbey Posted January 3, 2012 Share Posted January 3, 2012 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>'); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/254270-how-to-run-php-in-a-html-file/#findComment-1303688 Share on other sites More sharing options...
usman07 Posted January 3, 2012 Author Share Posted January 3, 2012 is the closing bracket meant to be after 'username' or after 'post'?thx Quote Link to comment https://forums.phpfreaks.com/topic/254270-how-to-run-php-in-a-html-file/#findComment-1303689 Share on other sites More sharing options...
usman07 Posted January 3, 2012 Author Share Posted January 3, 2012 is the closing bracket meant to be after "username"> or after method="post"/> thx Quote Link to comment https://forums.phpfreaks.com/topic/254270-how-to-run-php-in-a-html-file/#findComment-1303690 Share on other sites More sharing options...
Jacbey Posted January 3, 2012 Share Posted January 3, 2012 is the closing bracket meant to be after 'username' or after 'post'?thanks After the whole thing before ?> I put it in in the post above. Quote Link to comment https://forums.phpfreaks.com/topic/254270-how-to-run-php-in-a-html-file/#findComment-1303691 Share on other sites More sharing options...
usman07 Posted January 3, 2012 Author Share Posted January 3, 2012 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! Quote Link to comment https://forums.phpfreaks.com/topic/254270-how-to-run-php-in-a-html-file/#findComment-1303692 Share on other sites More sharing options...
Jacbey Posted January 3, 2012 Share Posted January 3, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/254270-how-to-run-php-in-a-html-file/#findComment-1303693 Share on other sites More sharing options...
AyKay47 Posted January 3, 2012 Share Posted January 3, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/254270-how-to-run-php-in-a-html-file/#findComment-1303695 Share on other sites More sharing options...
usman07 Posted January 3, 2012 Author Share Posted January 3, 2012 the problem i have now is that my username: box and password:box have disappeared and it just displays 'password incorrect!' Quote Link to comment https://forums.phpfreaks.com/topic/254270-how-to-run-php-in-a-html-file/#findComment-1303699 Share on other sites More sharing options...
Jacbey Posted January 3, 2012 Share Posted January 3, 2012 It works absolutely fine for me, are you sure you haven't done anything that would make it stop working? Copy and paste the WHOLE index.php and I'll take a look. Quote Link to comment https://forums.phpfreaks.com/topic/254270-how-to-run-php-in-a-html-file/#findComment-1303700 Share on other sites More sharing options...
usman07 Posted January 3, 2012 Author Share Posted January 3, 2012 i may have done something wrong, i will now really appreciate ur help Quote Link to comment https://forums.phpfreaks.com/topic/254270-how-to-run-php-in-a-html-file/#findComment-1303702 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.