PHP Question
remiX 19 Oct 2012
Hey everyone, I'm in the stage of learning PHP and coding a register/login website />
I need some help though:
I'm just using a text file to store the usernames and passwords just for now for testing and I'm having trouble when it checks if the username already exists or not. Yes it does work but it is case sensitive.
I'm using this code to read usernames and password - it does work fine.
Example: If I register account 'test'. And then register again as account 'test' it will fail - but if i register with account 'tEst' it will succeed because it checks if the text in the file is EXACTLY equal to the username entered.
So my question: How can I prevent this?
- I was thinking of when it checks the line in the file and username to change it into lowercase so it isn't case senstive.
Is this the best option? If it is, how exactly do I make a variable into lowercase?
Thanks.
I need some help though:
I'm just using a text file to store the usernames and passwords just for now for testing and I'm having trouble when it checks if the username already exists or not. Yes it does work but it is case sensitive.
I'm using this code to read usernames and password - it does work fine.
$fp = fopen ( 'users.txt', 'rb' ); while ($line = fgetcsv($fp,100,",")) { echo "Username: ".$line[0]." || Password: ".$line[1]."<br>"; -- I have this line so it prints each username and password that is in the file if ( ($line[0] == $username) ) { echo "<b>Sorry! That user (".$username.") has already been used.</b>"; fclose ( $fp ); exit; } }
Example: If I register account 'test'. And then register again as account 'test' it will fail - but if i register with account 'tEst' it will succeed because it checks if the text in the file is EXACTLY equal to the username entered.
So my question: How can I prevent this?
- I was thinking of when it checks the line in the file and username to change it into lowercase so it isn't case senstive.
Is this the best option? If it is, how exactly do I make a variable into lowercase?
Thanks.
stan2012 19 Oct 2012
to make string lowercase use:
$line[0] = strtolower($line[0])
$username = strtolower($username)
$line[0] = strtolower($line[0])
$username = strtolower($username)
Espen 19 Oct 2012
What you want is strcasecmp():
http://de2.php.net/m....strcasecmp.php
EDIT: Sorry, I misunderstood you and thought you wanted exactly the opposite. Another *derp*, this is not my day.^^
EDIT2: Not wait, I DID understand you correctly! *derpy-bird-flock*
EDIT 3: Also in general, when comparing strings you should try to avoid doing it with == or else there might be unintended consequences.
The reason for that is explained here: http://www.php.net/m....comparison.php (especially the little text and the code example there)
So either use the tertiary operator === (if you can guarantee that the variables to be compared will actually be strings!), or always use strcmp() and strcasecmp() which is the preferred and most safe choice when comparing strings.
Edited by Espen, 19 October 2012 - 03:42 PM.
http://de2.php.net/m....strcasecmp.php
EDIT2: Not wait, I DID understand you correctly! *derpy-bird-flock*
EDIT 3: Also in general, when comparing strings you should try to avoid doing it with == or else there might be unintended consequences.
The reason for that is explained here: http://www.php.net/m....comparison.php (especially the little text and the code example there)
So either use the tertiary operator === (if you can guarantee that the variables to be compared will actually be strings!), or always use strcmp() and strcasecmp() which is the preferred and most safe choice when comparing strings.
Edited by Espen, 19 October 2012 - 03:42 PM.
Kolpa 19 Oct 2012
also FFS DON'T CREATE A FILE ITS AN WEB SERVER EVERYBODY THAT KNOWS THE FILE NAME GETS ALL THE DATA
use an database that way the data access is safer and easier aswell
use an database that way the data access is safer and easier aswell
remiX 19 Oct 2012
Kolpa, on 19 October 2012 - 05:42 PM, said:
also FFS DON'T CREATE A FILE ITS AN WEB SERVER EVERYBODY THAT KNOWS THE FILE NAME GETS ALL THE DATA
use an database that way the data access is safer and easier aswell
use an database that way the data access is safer and easier aswell
What? I'm using my local machine to use and test this. I'm not using it for a website now. I just starting learning php a few days ago.
Btw it's A database, not an.
stan2012, on 19 October 2012 - 03:18 PM, said:
to make string lowercase use:
$line[0] = strtolower($line[0])
$username = strtolower($username)
$line[0] = strtolower($line[0])
$username = strtolower($username)
Thanks. Going to use this because it's the easiest />
Kolpa 20 Oct 2012
$fp = fopen ( 'users.txt', 'rb')
this is an local directory meaning if somebody changes the url from whaterver/login.php to whatever/users.txt he is able to read the user file
which is an pretty big security issue
so i recommend you to use databases for storing stuff like that, also it makes the data handling way easier because you don't need to create the parsing yourself
this is an local directory meaning if somebody changes the url from whaterver/login.php to whatever/users.txt he is able to read the user file
which is an pretty big security issue
so i recommend you to use databases for storing stuff like that, also it makes the data handling way easier because you don't need to create the parsing yourself
Melerion 20 Oct 2012
While I do commend all the helpful people who contributed a solution I still think that this page is not the right forum for PHP questions.
I'd also like to point out that PHP has excellent documentation available on http://php.net/
It has nothing to do with computercraft or LUA, though I have to admit that this was clear enough from the subject of this post.
If I'm overstepping, feel free to flame me.
I'd also like to point out that PHP has excellent documentation available on http://php.net/
It has nothing to do with computercraft or LUA, though I have to admit that this was clear enough from the subject of this post.
If I'm overstepping, feel free to flame me.
remiX 20 Oct 2012
Kolpa, on 20 October 2012 - 09:48 AM, said:
$fp = fopen ( 'users.txt', 'rb')
this is an local directory meaning if somebody changes the url from whaterver/login.php to whatever/users.txt he is able to read the user file
which is an pretty big security issue
so i recommend you to use databases for storing stuff like that, also it makes the data handling way easier because you don't need to create the parsing yourself
this is an local directory meaning if somebody changes the url from whaterver/login.php to whatever/users.txt he is able to read the user file
which is an pretty big security issue
so i recommend you to use databases for storing stuff like that, also it makes the data handling way easier because you don't need to create the parsing yourself
Yeah I know /> I'm currently only using this code on my local PC.
Is there a way I can use MySQL on my local pc? I'm using xampp
cant_delete_account 20 Oct 2012
Orwell 21 Oct 2012
remiX 21 Oct 2012
Orwell, on 21 October 2012 - 03:34 AM, said:
Haha cool /> How do I connect to a mysql database and read/write from/to it?
Espen 21 Oct 2012
sIdEkIcK_, on 21 October 2012 - 10:40 AM, said:
-snip-
Haha cool /> How do I connect to a mysql database and read/write from/to it?
Haha cool /> How do I connect to a mysql database and read/write from/to it?
Since I'm using XAMPP rather extensively at the moment, I happen to have a bookmark at the ready:
http://www.apachefri.../faq-xampp.html
Especially take a look at "Installing, Configuring, and Developing with XAMPP" by Dalibor Dvorski at the very bottom of the page.
It's a PDF file with an illustrative tutorial for how to setup XAMPP incl. MySQL and it even has some basic example for how to connect to MySQL via PHP, etc.
Hope it helps,
Cheers />
Kolpa 21 Oct 2012
here is an php snippet of some database connection i did https://github.com/K...e/ListFiles.php
for the query strings there is this website http://www.w3schools...sql/default.asp
for the query strings there is this website http://www.w3schools...sql/default.asp
remiX 21 Oct 2012
Cloudy, on 21 October 2012 - 10:47 AM, said:
Google works wonders.
Google, what's that? I know only of bing.
Spoiler
Espen, on 21 October 2012 - 12:04 PM, said:
Since I'm using XAMPP rather extensively at the moment, I happen to have a bookmark at the ready:
http://www.apachefri.../faq-xampp.html
Especially take a look at "Installing, Configuring, and Developing with XAMPP" by Dalibor Dvorski at the very bottom of the page.
It's a PDF file with an illustrative tutorial for how to setup XAMPP incl. MySQL and it even has some basic example for how to connect to MySQL via PHP, etc.
Hope it helps,
Cheers />
http://www.apachefri.../faq-xampp.html
Especially take a look at "Installing, Configuring, and Developing with XAMPP" by Dalibor Dvorski at the very bottom of the page.
It's a PDF file with an illustrative tutorial for how to setup XAMPP incl. MySQL and it even has some basic example for how to connect to MySQL via PHP, etc.
Hope it helps,
Cheers />
Awesome, this will be very helpful to me. Thanks
PS: Is it xamp, xampp or xammp. xD I bookmarked it as xammp o.O
Kolpa, on 21 October 2012 - 03:31 PM, said:
here is an php snippet of some database connection i did https://github.com/K...e/ListFiles.php
for the query strings there is this website http://www.w3schools...sql/default.asp
for the query strings there is this website http://www.w3schools...sql/default.asp
Thanks />
Espen 21 Oct 2012
remiX 22 Oct 2012
Espen, on 21 October 2012 - 05:52 PM, said:
Makes more sense now.
2 more questions: How do I make a part of a code update every second (like a code to show the time)?
And how do I make the code go to a different file after exit; / or a few seconds?
if (empty ($username)) { print "Please enter a username." ; exit; } // Checks to see if a username has been enteredLike I have this code and where it says exit I want it to go back to the login page. If this make any sense.
Espen 22 Oct 2012
It's been some years for me with PHP, but I believe you can redirect to a new page by using header("Location:").
EDIT: Just googled some links, these might be useful to look at:
http://de3.php.net/m...tion.header.php
http://www.phpeasyst...om/phptu/6.html
http://www.html-form...login-form.html
Edited by Espen, 22 October 2012 - 08:05 AM.
EDIT: Just googled some links, these might be useful to look at:
http://de3.php.net/m...tion.header.php
http://www.phpeasyst...om/phptu/6.html
http://www.html-form...login-form.html
Edited by Espen, 22 October 2012 - 08:05 AM.
remiX 22 Oct 2012
Awesome /> the header thing works. Although sleeping seems to make it not show the error.
I'm using this and I have sleep after it prints the text but it doesn't print the text at all. It does wait and then goes back to signup.php.
And how do I make a part of a code update every second (like a code to show the time)?
if (empty ($username)) { print "Please enter a username."; sleep(3); header("Location: http://localhost/lol/signup.php"); exit; }
I'm using this and I have sleep after it prints the text but it doesn't print the text at all. It does wait and then goes back to signup.php.
And how do I make a part of a code update every second (like a code to show the time)?