This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
remiX's profile picture

PHP Question

Started by remiX, 19 October 2012 - 01:58 PM
remiX #1
Posted 19 October 2012 - 03:58 PM
Hey everyone, I'm in the stage of learning PHP and coding a register/login website :P/>/>

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 #2
Posted 19 October 2012 - 04:18 PM
to make string lowercase use:

$line[0] = strtolower($line[0])
$username = strtolower($username)
Espen #3
Posted 19 October 2012 - 04:21 PM
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 on 19 October 2012 - 02:42 PM
Kolpa #4
Posted 19 October 2012 - 06:42 PM
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
remiX #5
Posted 19 October 2012 - 07:58 PM
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

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.

to make string lowercase use:

$line[0] = strtolower($line[0])
$username = strtolower($username)

Thanks. Going to use this because it's the easiest :P/>/>
Kolpa #6
Posted 20 October 2012 - 10:48 AM
$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
Melerion #7
Posted 20 October 2012 - 06:16 PM
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.
remiX #8
Posted 20 October 2012 - 11:53 PM
$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

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 #9
Posted 21 October 2012 - 12:14 AM
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.
General: Discuss Anything and Everything Relating to Anything and Everything
Melerion #10
Posted 21 October 2012 - 04:31 AM
I stand corrected.
Orwell #11
Posted 21 October 2012 - 04:34 AM
* snip *

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

Yes, that's what the 'm' in xamp/lamp/wamp stands for. : ) Just create a database on the local mysql server and connect to it on localhost from within php.
remiX #12
Posted 21 October 2012 - 11:40 AM
* snip *

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

Yes, that's what the 'm' in xamp/lamp/wamp stands for. : ) Just create a database on the local mysql server and connect to it on localhost from within php.

Haha cool :)/>/> How do I connect to a mysql database and read/write from/to it?
Cloudy #13
Posted 21 October 2012 - 11:47 AM
Google works wonders.
Espen #14
Posted 21 October 2012 - 01:04 PM
-snip-
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.&#46;&#46;/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 #15
Posted 21 October 2012 - 04:31 PM
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.com/sql/default.asp
remiX #16
Posted 21 October 2012 - 05:19 PM
Google works wonders.

Google, what's that? I know only of bing.
Spoiler:)/>/>

Since I'm using XAMPP rather extensively at the moment, I happen to have a bookmark at the ready:
http://www.apachefri.&#46;&#46;/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

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

Thanks :)/>/>
Espen #17
Posted 21 October 2012 - 06:52 PM
PS: Is it xamp, xampp or xammp. xD I bookmarked it as xammp o.O

It's XAMPP:
X - "Cross"-Platform (Windows, Linux, Mac, etc. In the past there were separate versions called WAMPP and LAMPP)
A - Apache
M - MySQL
P - PHP
P - Perl
remiX #18
Posted 22 October 2012 - 02:03 AM
PS: Is it xamp, xampp or xammp. xD I bookmarked it as xammp o.O

It's XAMPP:
X - "Cross"-Platform (Windows, Linux, Mac, etc. In the past there were separate versions called WAMPP and LAMPP)
A - Apache
M - MySQL
P - PHP
P - Perl

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 entered
Like 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 #19
Posted 22 October 2012 - 09:04 AM
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/manual/en/function.header.php
http://www.phpeasystep.com/phptu/6.html
http://www.html-form-guide.com/php-form/php-login-form.html
Edited on 22 October 2012 - 07:05 AM
remiX #20
Posted 22 October 2012 - 12:38 PM
Awesome :)/>/> the header thing works. Although sleeping seems to make it not show the error.

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)?
Kolpa #21
Posted 22 October 2012 - 05:25 PM
you are getting the use of php wrong
php is not java script or anything like that php executes one time when you open the page and then never again