Jump to content

Newbie question

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
20 replies to this topic

#1
Flezria

Flezria

    Learning Programmer

  • Members
  • PipPipPip
  • 55 posts
Hello, i really dont know why this piece of my code doesnt work, its really easy piece of code. the problem is that its saying my username and pass are wrong, anyone could tell me why?
<?php


$username = $_POST['username'];

$password = $_POST['password'];


if($username == "user" && $password == "pass") {

  

      $_SESSION['user'] = $username;

      $_SESSION['logged'] = "yes";


      

}


else{


      print "username or pass was incorrect!";

   

     }

     

?>

Thanks

#2
Orjan

Orjan

    Writes binary right handed and hex left handed

  • Moderators
  • 3,299 posts
you tell there that your username is "user" and the password is "pass", if you don't give that info, and make sure you submit your form with the POST method, it will tell it's not correct.

as well, usage of the $_SESSION variable needs you to start the session before any output is done with session_start();
__________________________________________
I study Information Systems at Karlstad University when I'm not on CodeCall

#3
Flezria

Flezria

    Learning Programmer

  • Members
  • PipPipPip
  • 55 posts
Thanks for your reply, i started the session, but its still wont work, and the submit form is working aswell.

Submit form:
<html>


<head>

<form action="adminact.php" type="post">
<table>
<tr>
<td>username: </td>
<td><input type="text" name="username"></td>
</tr>
<tr>
<td>password: </td>
<td><input type="password" name="password"></td>
</tr>
<tr>
<td><input type="submit" name="submit"></td>
</tr>
</table>
</form>

</head>

<body>

</body>


</html>


adminact:
<?php

session_start();

$username = $_POST['username'];
$password = $_POST['password'];

if($username == "user" && $password == "pass") {
  
      $_SESSION['user'] = $username;
      $_SESSION['logged'] = "yes";

      
}

else{

      print "username or pass was incorrect!";
   
     }
     
?>


#4
Orjan

Orjan

    Writes binary right handed and hex left handed

  • Moderators
  • 3,299 posts
The form shall not be in the head part of your html document.
try to change to method=get and use $_GET instead, and see what happens. do you get your data correct in your url then?
__________________________________________
I study Information Systems at Karlstad University when I'm not on CodeCall

#5
Flezria

Flezria

    Learning Programmer

  • Members
  • PipPipPip
  • 55 posts
That worked, very cool :) but in the url i get username=user&password=pass&submit=Submit+Query can u tell me how to fix it? im getting it with post too

#6
Orjan

Orjan

    Writes binary right handed and hex left handed

  • Moderators
  • 3,299 posts
um, it should only be there with GET, not with POST...
__________________________________________
I study Information Systems at Karlstad University when I'm not on CodeCall

#7
Flezria

Flezria

    Learning Programmer

  • Members
  • PipPipPip
  • 55 posts
I know, thats why its wierd :confused:

#8
Orjan

Orjan

    Writes binary right handed and hex left handed

  • Moderators
  • 3,299 posts
what's your HTML now?
__________________________________________
I study Information Systems at Karlstad University when I'm not on CodeCall

#9
Flezria

Flezria

    Learning Programmer

  • Members
  • PipPipPip
  • 55 posts
Its
<html>


<head>

</head>

<body>

<form action="adminact.php" method="post">
<table>
<tr>
<td>username: </td>
<td><input type="text" name="username"></td>
</tr>
<tr>
<td>password: </td>
<td><input type="password" name="password"></td>
</tr>
<tr>
<td><input type="submit" name="submit"></td>
</tr>
</table>
</form>

</body>


</html>

I made some changes, but its still buggy, with the post i get both true and false(if and else):

<?php

session_start();

$username = $_POST['username'];
$password = $_POST['password'];

if($username == "user" && $password == "pass") {
  
      $_SESSION['user'] = $username;
      $_SESSION['logged'] = "yes";

      
}

else {

      print "username or pass was incorrect!";
   
     }

if($_SESSION['logged'] == "yes") {
	
	echo "you are now logged in!";
	
}

?>

its giving me username or pass was incorrect!you are now logged in!

#10
Orjan

Orjan

    Writes binary right handed and hex left handed

  • Moderators
  • 3,299 posts
if you don't know if it's GET or POST, you can always use $_REQUEST, as that contains both GET and POST variables
__________________________________________
I study Information Systems at Karlstad University when I'm not on CodeCall

#11
Flezria

Flezria

    Learning Programmer

  • Members
  • PipPipPip
  • 55 posts
Yes, but i still got the url problem, its really annoying :D

#12
Flezria

Flezria

    Learning Programmer

  • Members
  • PipPipPip
  • 55 posts
Yes, but i still got the url problem, its really annoying :D