Reputation:
I have just started learning PHP and I cant seem to to work like this is not going to welcome.php
please help? Thankyou for your help as I have litterally put my head into this :(
$user=$_POST['user'];
$pass=$_POST['pass'];
$que ="SELECT * FROM users WHERE username='$user' and password='$pass'";
$res=mysql_query($que);
$c=mysql_num_rows($res);
if($c==1){
$_SESSION['user'] = $user;
header("location: welcome.php");
}
else {
header("location: empty.php");
}
?>
<form name="myform" method="POST" action="login.php">
<input name="username" type="text" id="username">
<input name="pass" input type="password" id="pass">
<input type="submit" name="Submit" value="Login">
</form>
Upvotes: 1
Views: 64
Reputation: 368
Try this please:
<?php
session_start(); // You need this if you use a session
$user = mysql_escape_string($_POST['user']); // Defend from SQL Injection
$pass = mysql_escape_string($_POST['pass']);
if(isset($_POST['Submit']))
{
$que = "SELECT * FROM users WHERE username='$user' and password='$pass';";
$res = mysql_query($que);
$c = mysql_num_rows($res);
if($c==1){ // if you want use a session you need add session_start();
$_SESSION['user'] = $user;
header("location: welcome.php");
}
else {
header("location: empty.php");
}
}
?>
<form name="myform" method="POST" action="login.php">
<input name="username" type="text" id="username">
<input name="pass" type="password" id="pass">
<input type="submit" name="Submit" value="Login">
</form>
Upvotes: 1
Reputation: 16792
If you have properly used session_start();
It has already been mentioned where you have messed up, I will however post it for you so you can copy/paste and see it for yourself :) Furthermore, Beware of SQL Injection/XSS
Study PDO/msqli
<?php
session_start();
if(isset($_POST['Submit']){
$user=$_POST['user'];
$pass=$_POST['pass'];
$que ="SELECT * FROM users WHERE username='$user' and password='$pass'";
$res=mysql_query($que);
$c=mysql_num_rows($res);
if($c==1){
$_SESSION['user'] = $user;
header("location: welcome.php");
}
else {
header("location: empty.php");
}
}
?>
<form name="myform" method="POST" action="login.php">
<input name="user" type="text">
<input name="pass" input type="password">
<input type="submit" name="Submit" value="Login">
</form>
Upvotes: 0