Jump to content




PHP Question


20 replies to this topic

#1 remiX

  • Members
  • 2,076 posts
  • LocationSouth Africa

Posted 19 October 2012 - 02: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.

#2 stan2012

  • New Members
  • 10 posts

Posted 19 October 2012 - 03:18 PM

to make string lowercase use:

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

#3 Espen

    Curious Explorer

  • Members
  • 708 posts

Posted 19 October 2012 - 03: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 by Espen, 19 October 2012 - 03:42 PM.


#4 Kolpa

  • New Members
  • 260 posts
  • LocationGermany

Posted 19 October 2012 - 05: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

#5 remiX

  • Members
  • 2,076 posts
  • LocationSouth Africa

Posted 19 October 2012 - 06:58 PM

 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

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)

Thanks. Going to use this because it's the easiest :P/>

#6 Kolpa

  • New Members
  • 260 posts
  • LocationGermany

Posted 20 October 2012 - 09: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

#7 Melerion

  • New Members
  • 7 posts

Posted 20 October 2012 - 05: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.

#8 remiX

  • Members
  • 2,076 posts
  • LocationSouth Africa

Posted 20 October 2012 - 10:53 PM

 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

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

#9 cant_delete_account

  • Members
  • 484 posts

Posted 20 October 2012 - 11:14 PM

 Melerion, on 20 October 2012 - 05:16 PM, said:

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

#10 Melerion

  • New Members
  • 7 posts

Posted 21 October 2012 - 03:31 AM

I stand corrected.

#11 Orwell

    Self-Destructive

  • Members
  • 1,091 posts

Posted 21 October 2012 - 03:34 AM

 sIdEkIcK_, on 20 October 2012 - 10:53 PM, said:

 Kolpa, on 20 October 2012 - 09:48 AM, said:

* 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.

#12 remiX

  • Members
  • 2,076 posts
  • LocationSouth Africa

Posted 21 October 2012 - 10:40 AM

 Orwell, on 21 October 2012 - 03:34 AM, said:

 sIdEkIcK_, on 20 October 2012 - 10:53 PM, said:

 Kolpa, on 20 October 2012 - 09:48 AM, said:

* 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?

#13 Cloudy

    Ex-Developer

  • Members
  • 2,543 posts

Posted 21 October 2012 - 10:47 AM

Google works wonders.

#14 Espen

    Curious Explorer

  • Members
  • 708 posts

Posted 21 October 2012 - 12:04 PM

 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?

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 :)/>

#15 Kolpa

  • New Members
  • 260 posts
  • LocationGermany

Posted 21 October 2012 - 03: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...sql/default.asp

#16 remiX

  • Members
  • 2,076 posts
  • LocationSouth Africa

Posted 21 October 2012 - 04:19 PM

 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 :)/>

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

Thanks :)/>

#17 Espen

    Curious Explorer

  • Members
  • 708 posts

Posted 21 October 2012 - 05:52 PM

 sIdEkIcK_, on 21 October 2012 - 04:19 PM, said:

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

#18 remiX

  • Members
  • 2,076 posts
  • LocationSouth Africa

Posted 22 October 2012 - 01:03 AM

 Espen, on 21 October 2012 - 05:52 PM, said:

 sIdEkIcK_, on 21 October 2012 - 04:19 PM, said:

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.

#19 Espen

    Curious Explorer

  • Members
  • 708 posts

Posted 22 October 2012 - 08: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/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.


#20 remiX

  • Members
  • 2,076 posts
  • LocationSouth Africa

Posted 22 October 2012 - 11:38 AM

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)?





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users