PHP Error with creating Log In

Warning: Cannot modify header information - headers already sent by (output started at /storage/ssd5/582/2185582/public_html/login.php:6) in /storage/ssd5/582/2185582/public_html/login.php on line 28

That is my error. I followed a tutorial, but I must have messed up along the way.

Here is my code:

<?php ob_start(); $con = mysql_connect("localhost", "id2185582_uwrfiv", "xxxxxxxxxxx"); if (!$con){ die('Could not connect:' .mysql_error()); } mysql_select_db("id2185582_studentdb", $con);

$user = $_POST[“username”];
$pwd = $_POST[“password”];

$user = stripslashes($user);
$pwd = stripslashes($pwd);
$user = mysql_real_escape_string($user);
$pwd = mysql_real_escape_string($pwd);
$sql=“SELECT * FROM Users WHERE username=’$user’ and password=’$pwd’”;
$result=mysql_query($sql);

$count=mysql_num_rows($result);
if($count==1){
$_SESSION[‘user’]= “user”;
$_SESSION[‘pwd’]= “pwd”;
header(“location:studentdatabaseview.php”);
}
else {
echo “Wrong Username or Password”;
}
ob_end_flush();
?>

Any tips or ideas what is wrong? I am using PHP 5.6. It said it is compatible with it(on the tutorial).

@stevenhayes97 php_flag output_buffering on
Add above piece of code in your .htaccess file and then try again.

This should solve your issue.

That solved that issue, but it created a small new one. It is related so you think that you could answer? It accepted the username and password and the url changed, but the screen didn’t change and it didn’t redirect me to where it was supposed to go.

I have this on the page it was supposed to go to:

<?php if(!isset($_SESSION['user'])){ header("Location:DBInterface.html"); } ?>

I take back the changing URL part. The page loads for a second after accepting the correct credentials then doesn’t do anything else.

What is your website name??

is this the one “uwrfintervarsity.000webhostapp.com”??

1 Like

http://www.uwrfintervarsity.com/

Yes that is it. Am I just missing some code to redirect to the new page after logged in successfully?

Let me check what’s the issue and then i’ll get back to you.

1 Like

@stevenhayes97 Sry was bit busy,
First it’s not allowing me to log in.
There seems to be issue with after database query in you script, can you check and then try again.

I think the issue is in here:

$count=mysql_num_rows($result);
if{
$_SESSION[‘user’]= “user”;
$_SESSION[‘pwd’]= “pwd”;
header(“location:studentdatabaseview.php”);
}
else {
echo “Wrong Username or Password”;
}

I am playin garound with it

I changed the code to this:

$count=mysql_num_rows($result);

if($count==1){

session_register(“user”);
session_register(“pwd”);
header(“location:studentdatabaseview.php”);
}
else {
echo “Wrong Username or Password”;
}
ob_end_flush();

It does the else statement when the password is correct so it gets that far.

Now this is the error:

Call to undefined function session_register() in /storage/ssd5/582/2185582/public_html/login.php on line 28

Try out this tutorial

1 Like

Call to undefined function session_register()

session_register() is a very old function which has been ruled out since PHP 5.3 - 5.4

If you want to continue with your code you’ll have to change the PHP version: 000webhost cPanel > Settings > General > Change PHP version > 5.3

1 Like

Thanks I will give that a try if all else fails.

I got rid of all the session_register() functions.

I tried this; session_start();
if(!isset($_SESSION[‘username’])){
header(“Location:Login.php”);
}

But it just redirects me back to log in page no matter what.

It’s hard to debug the entire code. Also, I see you are using old functions such as mysql_*, which are deprecated and ruled out on newer PHP versions.

Please follow @ckhawand’s tutorial :wink:

2 Likes

Alright thanks for your help I will!

@stevenhayes97 Let us know if you face any issue, we’ll try to help you out :slight_smile:

Thanks I am close. I followed all the steps and I created an email address in the database with all required things, but it just tells me “incorrect credentials”. The only thing I changed was the destination after correctly logging in.

I’ll investigate your issue
You cannot add people like that to the database, the passwords are hashed before they are sent to the database, so add the signup page, register the users, then delete it :wink: