
Originally Posted by
Bolkonsky
Thanks for the response!
I'll keep that in mind. From now on: Declare all variables at top of page. (When possible.)
It doesn't have to be at the top of the page. Just make sure you declare it sometime before you use it. E.g., don't do stuff like
PHP Code:
function my_function() {
if (something()) {
$foo = " there";
}
return "Hello$foo!";
}
I mean, you can do it. It will reliably return "Hello!" if something() returned false, and "Hello there!" if something() returned true. Some PHP programmers do stuff like that. But it's a better idea IMO to set error_reporting(E_ALL | E_STRICT); at the top of every page, and make sure you fix all the errors. It helps avoid bugs.

Originally Posted by
Bolkonsky
Does this include comments? Also, will PHP script on the page (below the redirect) still execute?[/DEL] (EDIT: I'm an idiot. Just thought of a real easy way to test this. 2nd EDIT: Yes, it does appear to execute.)
The PHP script will always execute in full no matter what you output. An HTTP redirect created with header() is just a special type of response, which tells the browser to go to the given location instead of displaying something. It behaves like a normal HTTP response in all other ways.

Originally Posted by
Bolkonsky
One final question.
Does MySQL index rows? For instance, I have:
[/DEL]
Now in the first row, there's my email, my username, and my password. Could I theoretically find the row that has "COGlory" as it's username, (we'll say row1) and then get the value from the email and password in row1? I can't find anything on this in any tutorials.
Yes:
Code:
SELECT email, password FROM table_name WHERE username='COGlory';
If you don't tell MySQL otherwise, this works by MySQL just scanning through all the rows until it finds all the ones matching the WHERE clause, which it then returns. This is fine for small tables, but for larger tables (thousands of rows or more) it can be slow. Then you want to add an index like so:
Code:
ALTER TABLE table_name ADD INDEX (username);
Any decent introduction to MySQL should explain this.
I should also add that you really need to escape any strings you pass to MySQL. Otherwise your program will break if people use special characters like ' or \. For instance, consider the following:
PHP Code:
$result = mysql_query("SELECT email, password FROM table_name WHERE username='" . $_GET['username'] . "'");
If $_GET['username'] is "COGlory", this will work fine. But if the user submits a name like "Apos'trophe", then it will become
PHP Code:
$result = mysql_query("SELECT email, password FROM table_name WHERE username='Apos'trophe'");
which is a syntax error. Worse, an attacker could submit a malicious name to make the query do something entirely different (an "SQL injection attack"). The correct way to do this is
PHP Code:
$result = mysql_query("SELECT email, password FROM table_name WHERE username='" . mysql_real_escape($_GET['username']) . "'");
mysql_real_escape() serves a similar purpose to htmlspecialchars() for HTML.

Originally Posted by
Bolkonsky
I think I'm just gonna buy a book. (Which if I do, does anyone have recommendations?)
Unfortunately, I don't know a good intro MySQL book.

Originally Posted by
Bolkonsky
Well, here's another question to make up for me figuring those out. After the header, is there a way to redirect someone to a different webpage, after the header? For instance, if a login is successful, I'd like to forward the user to a different page, but if it's unsuccessful, I'd like to send them back to the index. Google can't seem to find this one.
Why don't you just send a different Location header depending on whether it was successful? E.g.,
PHP Code:
$successful = try_to_login();
if ( $successful ) {
header( 'Location: http://mysite.com/different_page.php' );
} else {
header( 'Location: http://mysite.com/index.php' );
}

Originally Posted by
Bolkonsky
By dynamic, I mean if the login's successful, I need it to redirect to one page, but if it's unsuccessful, it needs to redirect to an index.
The PHP script itself is dynamic. You don't need to output the same thing every time, just output different redirects in the different cases.