mysql - PHP login form not working properly -
i trying create simple log in form, every time click on submit button goes other page, if log in info not correct. here code, can me problem? thanks!
login.php
<?php session_start(); if(isset($_session['username'])) { echo '<script type="text/javascript"> window.location="index2.php";</script>'; } if(isset($_post['submit'])) { include('connection.php'); $user = mysql_real_escape_string($_post['username']); $pass = md5($_post['password']); if($user && $pass) { $sql="select * korisnici username = '" . $user . "' , password = '" . $pass . "'"; if (!$q=mysql_query($sql)) { echo "<p>error</p>" . mysql_query(); die(); } if (mysql_num_rows($q)==0) { echo '<script type="text/javascript">alert("username password incorrect!"); window.location="login.php";</script>'; } else { $_session['username'] = $_post['username']; echo "<script language='javascript'>alert('everything works')</script>"; } } else { echo "<script language='javascript'>alert('fill out both fields!')</script>"; } } ?> <head> <?php require("connection.php") ?> </head> <body> <div id="wrapper"> <form action="login.php" method="post"> username: <input type="text" name="username"> password:  <input type="password" name="password"> <input type="submit" id="submit" value="login" name="submit" class="button" /> </form> </div> </body> </html>
connection.php
<?php $mysql_server = "localhost"; $mysql_user = "root"; $mysql_password = ""; $mysql_db = "comment"; if (!$db=mysql_connect($mysql_server, $mysql_user, $mysql_password)) { die ("<p>failed connect</p>"); } if (!mysql_select_db($mysql_db, $db)) { die ("<p>wrong database</p>"); } else { mysql_query("set names utf8"); mysql_query("set character set utf8"); mysql_query("set collation_connection='utf8_unicode_ci'"); } ?>
index2.php - testing if works
<p>welcome</p>
as mentioned in question, index2.php
file contains sample html text. have action
attribute set file, causes post values form sent script reason, why got redirected on file data incorrect.
<form action="index2.php" method="post">
should be
<form action="" method="post">
Comments
Post a Comment