[PHP] Please Help [PHP]

TrueJewix

Member
Aug 9, 2012
332
21
First of all i want to say sorry if this thread is in the wrong section well then please put it in the right section...

So im making a website with php (Im new too PHP) and iam coding a script
i want it to say Welcome {Username}! You've been successfully logged in, only if the user has logged in but this is what it does after i log in
and by the way i have the database and registered... Please Help
 

Sysode

Front-End Developer
Dec 11, 2012
1,673
848
Edit:

Just read the code properly..

Are you using sessions to store the logins? $_POST is used in forms, if you are using sessions you want to use $_SESSION['username']


Post some more of your code.
 

Markshall

Русский Стандарт
Contributor
Dec 18, 2010
2,637
2,389
I'm assuming connect.php contains your MySQL connection details, however, in your script you make no queries to your database such as finding out whether the user exists, that's assuming you even have a users table in the database.

You also call $row but don't define it, unless it's defined in connect.php thus making it global to the current file you're in since you've included connect.php.

What you should probably do is if(!empty($_POST['username'])) rather than $row['username']

Then try $_SESSION['username'] = $_POST['username']

And then do echo 'Welcome ' . $_SESSION['username'] . ' you have been logged in.';

Of course you will need to query your database to find out if the user exists, and then also sanitize your queries and outputs to the browser to prevent XSS injections.
 

IntactDev

Member
Nov 22, 2012
399
71
You're getting that error because you didn't place any quotes around $_POST[username] and $_SESSION[username]

It should be:
$_POST['username']
$_SESSION['username']
 

TrueJewix

Member
Aug 9, 2012
332
21
Edit:

Just read the code properly..

Are you using sessions to store the logins? $_POST is used in forms, if you are using sessions you want to use $_SESSION['username']


Post some more of your code.

Thanks for the Advice

I'm assuming connect.php contains your MySQL connection details, however, in your script you make no queries to your database such as finding out whether the user exists, that's assuming you even have a users table in the database.

You also call $row but don't define it, unless it's defined in connect.php thus making it global to the current file you're in since you've included connect.php.

What you should probably do is if(!empty($_POST['username'])) rather than $row['username']

Then try $_SESSION['username'] = $_POST['username']

And then do echo 'Welcome ' . $_SESSION['username'] . ' you have been logged in.';

Of course you will need to query your database to find out if the user exists, and then also sanitize your queries and outputs to the browser to prevent XSS injections.


Thanks i tried it give me this error Parse error: syntax error, unexpected 'if' (T_IF) in C:\xampp\htdocs\index.php on line 29
could you please write the script?


You're getting that error because you didn't place any quotes around $_POST[username] and $_SESSION[username]

It should be:
$_POST['username']
$_SESSION['username']
Thanks for the advice
 
Last edited by a moderator:

Sysode

Front-End Developer
Dec 11, 2012
1,673
848
Thanks i tried it give me this error Parse error: syntax error, unexpected 'if' (T_IF) in C:\xampp\htdocs\index.php on line 29
could you please write the script?
Getting others to write your script won't help you learn. Post your code so we can edit it and show you what has been changed.
 

TrueJewix

Member
Aug 9, 2012
332
21
Getting others to write your script won't help you learn. Post your code so we can edit it and show you what has been changed.
Okay heres the script :
<?php
require_once("connect.php");
if(!empty($row[username])) // he got it.
{
$_SESSION[username] = $row[username];
echo "Welcome $_POST[username]! You've been successfully logged in!";
exit();
}
?>

Why don't you put your whole script on pastebin or pastie.... You have to help us help you.
 

Sysode

Front-End Developer
Dec 11, 2012
1,673
848
Okay heres the script :
<?php
require_once("connect.php");
if(!empty($row[username])) // he got it.
{
$_SESSION[username] = $row[username];
echo "Welcome $_POST[username]! You've been successfully logged in!";
exit();
}
?>


Post your connect.php as well, need to see what it includes. If it just includes a connection to the database then this script will not work as you haven't defined anything for $row.
 

TrueJewix

Member
Aug 9, 2012
332
21
Post your connect.php as well, need to see what it includes. If it just includes a connection to the database then this script will not work as you haven't defined anything for $row.
Heres my Connection.php
<?php
classMySQLDB
{
var $connection;//The MySQL database connection

/* Class constructor */
functionMySQLDB(){
/* Make connection to database */
$this->connection =@mysql_connect(localhost, whitey, abc123)ordie(mysql_error());
@mysql_select_db(blue, $this->connection)ordie(mysql_error());
}
/**
* query - Performs the given query on the database and
* returns the result, which may be false, true or a
* resource identifier.
*/
//Use this function as query("Query line of code");
function query($query){
return mysql_query($query, $this->connection);
}
};

$config =newMySQLDB;
?>
well thats the new one for now
 

Users who are viewing this thread

Top