|
|
|
|
![]() ![]() |
Mar 8 2008, 02:33 PM
Post
#1
|
|
|
Super Member ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Group: [HOSTED] Posts: 387 Joined: 9-February 08 Member No.: 57,615 |
Listen, as I'm novice in PHP, I will ask certain questions in this topic hoping on your contribution.
CODE $dbhost='.....'; CODE <?php //The Database Information// $dbhost='localhost'; $dbname='alex1985_test'; $dbusername='alex1985_admin'; $dbuserpass='0505009127'; //Creating Connection To The Database// mysql_connect ($dbhost, $dbusername, $dbuserpass); //Select The Certain Database// mysql_select_db ($dbname) or die ('Can Not Select Database'); CODE <?php //Start The Session// //Always Must Be On Top// session_start(); //Include Configuration File// include('config.php'); ?> There are questions which I derived practicing the coding: 1. Do you put spaces between words and brackets, as well as comas when you do coding. For instance, $dbhost, $dbusername. Is it right, or you do not have to use space between. 2. On some tutorials, the users have been used the character ', some of the are using ". What is the different between them. Can I use ' or ". or it does not matter everything. There are many aspects that I wanna ask you about. Could check the coding format, and tell me about the mistakes I did. Please, let me know as soon as possible. |
|
|
|
Mar 8 2008, 02:51 PM
Post
#2
|
|
|
apt-get moo ![]() Group: [MODERATOR] Posts: 2,056 Joined: 28-May 05 From: Hertfordshire, England Member No.: 7,593 ![]() |
CODE <?php //The Database Information// $dbhost='localhost'; $dbname='alex1985_test'; $dbusername='alex1985_admin'; $dbuserpass='0505009127'; //Creating Connection To The Database// mysql_connect ($dbhost, $dbusername, $dbuserpass); //Select The Certain Database// mysql_select_db ($dbname) or die ('Can Not Select Database'); Just a pointer, you don't need to finish a comment with //. There are two sorts of comments in PHP - single line and multi-line. The single line comment is started with a // and applies from that point until the end of the line. This means you can place it after a line of code, like this: CODE $username = 'alex1985'; // This is the username you log in with A multi-line comment applies over multiple lines, and does require you to finish it. It is started with /* and ends with */ The advantage, of course, is that you can have much longer comments without really long lines: CODE /* This function does something really cool. You can pass it all sorts of variables. Actually, it is quite pointless. */ function pointless(){ return true; } 1. Do you put spaces between words and brackets, as well as comas when you do coding. For instance, $dbhost, $dbusername. Is it right, or you do not have to use space between. It doesn't make any difference, but generally people put spaces in to make their code easier to read. For example, the second example here is much easier to read than the first example: CODE $dbh=mysql_connect($host,$username,$password)://Connect $dbh = mysql_connect($host, $username, $password); // Connect It is up to you to code how you want, but generally spaces are put after commas (i.e. in a list of parameters or variables) and around binary operators (=, +, -, *). 2. On some tutorials, the users have been used the character ', some of the are using ". What is the different between them. Can I use ' or ". or it does not matter everything. The single quote character takes its contents literally. Nothing placed in single quotes is parsed. This makes it faster, and more secure, but limits the uses. The double quote character parses its contents. That makes it slower, but a bit more useful. For example: CODE $number = 7; echo 'The \n number \n was... \n $number'; echo "The \n number \n was... \n $number"; Would output: QUOTE From the first echo (single quotes): The \n number \n was... \n $number From the second echo (double quotes): The number was... 7 |
|
|
|
Mar 8 2008, 03:30 PM
Post
#3
|
|
|
Super Member ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Group: [HOSTED] Posts: 387 Joined: 9-February 08 Member No.: 57,615 |
So, spaces are allowed?!
For instance, if(...some function...) and if (...some function...), is it right or wrong? Can use both of them or not? |
|
|
|
Mar 8 2008, 03:51 PM
Post
#4
|
|
|
apt-get moo ![]() Group: [MODERATOR] Posts: 2,056 Joined: 28-May 05 From: Hertfordshire, England Member No.: 7,593 ![]() |
You can use either of them and they will both work perfectly well. It depends entirely on your coding style as to which one you want to use. It is generally advised to add whitespace wherever it will make the code easier to read. So, if you look at a line, and you think it looks a little bit squashed, add some spaces in to make it easier to read.
Tabs are also a good idea to represent subsections of code. For example, in an if statement, the code that is executed is usually tabbed in, to separate it from the 'main' code: CODE if ($var == $var2)
{ echo 'They are the same'; } else { echo 'They are not the same'; } |
|
|
|
Mar 8 2008, 04:46 PM
Post
#5
|
|
|
Super Member ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Group: [HOSTED] Posts: 387 Joined: 9-February 08 Member No.: 57,615 |
OK. When you put equal sign in the coding, do I have to make spaces as well? Or generally both of them will be working?!
|
|
|
|
Mar 8 2008, 04:56 PM
Post
#6
|
|
|
apt-get moo ![]() Group: [MODERATOR] Posts: 2,056 Joined: 28-May 05 From: Hertfordshire, England Member No.: 7,593 ![]() |
As has been said, you do not need spaces. It will work whether they are there or not. However, they are generally added to make it easier to read.
|
|
|
|
Mar 8 2008, 08:37 PM
Post
#7
|
|
|
Super Member ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Group: [HOSTED] Posts: 387 Joined: 9-February 08 Member No.: 57,615 |
Thanks for your previous replies, were really helpful! How do I protect my user passwords in my database. If someone hacked the database, it was really hard for him to get passwords from that database. Please, list all good ways to do that.
|
|
|
|
Mar 8 2008, 09:17 PM
Post
#8
|
|
|
A computer once beat me at chess, but it was no match for me at kick boxing. ![]() Group: [MODERATOR] Posts: 3,882 Joined: 24-July 05 From: In Trouble Again... still? Member No.: 9,787 ![]() |
The most common method is to 'encrypt' the user_password before you store it into the file or Database.
Then you need to encrypt the input before you compare the entry to the stored value. If they encrypted input is the same as the encrypted stored value (using the same encryption method, then the user is validated. *EDIT* In register.php, this is the insert command I use: CODE $query = "INSERT INTO users ( first_name, last_name, email, password, registration_date, phone, cell, level, years, note) VALUES ( '$fn', '$ln', '$e', SHA1('$p'), NOW(), '$p', '$c', '$dl', '$y', '$n' )"; $result = @mysql_query ($query); // Run the query. if ($result) { // If it ran OK. And in the Login.php, here is the code for checking the password you get at log-in with the encryted one in the Database: CODE SELECT user_id, first_name, level FROM users WHERE email='$e' AND password=SHA1('$p') The password is selected based on the encrypted value, so in the Log-in script, handle the results based on the number of records returned. If zero, no member has that email and password. If one, the person should be allowed into the page/site. |
|
|