timlab55
Posts: 264
Joined: Sun Aug 01, 2021 1:52 am

Needing Some PHP Guidance

Fri Dec 02, 2022 5:34 am

I'm working on a website, and this website has numbers (dollars and cents). So with that being said here are my questions:
#1: In Mysqli do I make my variable varchar or what?
#2: If I make it varchar which will accept either a string or number, how should I go about it when it's in PHP to make it a number so that I can add both variables together to get $xxx.xx?
#3: Any other suggestions are welcome.

Thanks

ame
Posts: 6319
Joined: Sat Aug 18, 2012 1:21 am
Location: New Zealand

Re: Needing Some Guideance

Fri Dec 02, 2022 6:38 am

Seriously though, details are important.

You could use a decimal type. Or do everything in cents and divide by 100.
Hmm. What can I put here?

User avatar
rpdom
Posts: 21137
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Needing Some Guideance

Fri Dec 02, 2022 9:46 am

Don't store the currency symbol "$" in the database. Just add it in front of the number when displayed on the website.

PHP converts automatically between numeric strings "1.99" and numbers 1.99, but will give a warning and treat the value as 0 if it finds a non-numeric value like "$13.14".

For example:

Code: Select all

<?php

$a = "1.99";
$b = 2;
$c = "$3.14";

$d = $a + $b;
$e = $a + $c;

var_dump( $a, $b, $d, $e );
?>
results in

Code: Select all

PHP Warning:  A non-numeric value encountered in /tmp/fred on line 8
string(4) "1.99"
int(2)
float(3.99)
float(1.99)
Unreadable squiggle

timlab55
Posts: 264
Joined: Sun Aug 01, 2021 1:52 am

Re: Needing Some Guidance

Fri Dec 02, 2022 2:15 pm

All good, which brings me to the next question. And whoever did it, I know I misspelled "Guidance", figured I would get someone's attention. But thank you for doing it.
<?php

$a = "1.99";
$b = 2;
$c = "$3.14";

$d = $a + $b;
$e = $a + $c;

var_dump( $a, $b, $d, $e );
?>
With the error that I received "Notice: Undefined variable: e in example.php on line 10 string(4) "1.99" int(2) float(3.99) NULL, what would I need to put to correct the problem on the spot?
Thanks

User avatar
davidcoton
Posts: 6897
Joined: Mon Sep 01, 2014 2:37 pm
Location: Cambridge, UK

Re: Needing Some Guidance

Fri Dec 02, 2022 4:20 pm

timlab55 wrote:
Fri Dec 02, 2022 2:15 pm
All good, which brings me to the next question. And whoever did it, I know I misspelled "Guidance", figured I would get someone's attention. But thank you for doing it.
<?php

$a = "1.99";
$b = 2;
$c = "$3.14";

$d = $a + $b;
$e = $a + $c;

var_dump( $a, $b, $d, $e );
?>
With the error that I received "Notice: Undefined variable: e in example.php on line 10 string(4) "1.99" int(2) float(3.99) NULL, what would I need to put to correct the problem on the spot?
Thanks
Well, you are demonstrating the problem. Now take the $ out of the definition of $c:

$c = "3.14"

then try again.
Location: 345th cell on the right of the 210th row of L2 cache

timlab55
Posts: 264
Joined: Sun Aug 01, 2021 1:52 am

Re: Needing Some PHP Guidance

Sat Dec 03, 2022 5:18 am

I came to an end pass and worked on it for the last two days.
I have a database. In this database, I need one little item.
This item can be up on the first row or the last row, which will be governed by the WHERE clause, but the point is I need this little item.
So how would I write a Mysqli coding for it in PHP?

Thanks

ame
Posts: 6319
Joined: Sat Aug 18, 2012 1:21 am
Location: New Zealand

Re: Needing Some PHP Guidance

Sat Dec 03, 2022 5:35 am

timlab55 wrote:
Sat Dec 03, 2022 5:18 am
I came to an end pass and worked on it for the last two days.
I have a database. In this database, I need one little item.
This item can be up on the first row or the last row, which will be governed by the WHERE clause, but the point is I need this little item.
So how would I write a Mysqli coding for it in PHP?

Thanks
Can you stop talking in riddles?

Post code. Post data. Post a clear description of what you want to achieve.
Hmm. What can I put here?

timlab55
Posts: 264
Joined: Sun Aug 01, 2021 1:52 am

Re: Needing Some PHP Guidance

Sat Dec 03, 2022 5:38 am

ame - I agree with you but regarding the riddles, I am not. If I knew the coding why would I waste anyone's time? Besides getting the connection to the database, I have no clue how to write the query or anything.

As for the riddles, sorry.

ame
Posts: 6319
Joined: Sat Aug 18, 2012 1:21 am
Location: New Zealand

Re: Needing Some PHP Guidance

Sat Dec 03, 2022 6:04 am

timlab55 wrote:
Sat Dec 03, 2022 5:38 am
ame - I agree with you but regarding the riddles, I am not. If I knew the coding why would I waste anyone's time? Besides getting the connection to the database, I have no clue how to write the query or anything.

As for the riddles, sorry.
Usually one would post "I have X, but I need Y. How can I do it?"

Instead you have posted something that makes no sense, and makes it extremely hard to help.
Hmm. What can I put here?

User avatar
rpdom
Posts: 21137
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Needing Some PHP Guidance

Sat Dec 03, 2022 6:48 am

You say you have worked on this for two days and have nothing to show for it? Have you tried a simple internet search for PHP mysql? There are literally millions of examples out there. Just make sire you choose a recent one as PHP changed a bit.
Unreadable squiggle

timlab55
Posts: 264
Joined: Sun Aug 01, 2021 1:52 am

Re: Needing Some PHP Guidance

Sun Dec 04, 2022 12:28 am

This post is now closed. I reworked the problem and it is now solved. Again, I would like to thank all of you who help with this. I'm in the testing phase of my project now. There are a few things I do need to ask tho so please bear with me.

#1: Once a person enters my website and starts to do things and on most pages there are sessions, if that person doesn't log out correctly, do the sessions still go away after they leave?
#2: The same thing with variables, please?
#3: I found an interesting program called DiskIntemals and it's a Linux Reader for Windows. I'm telling you it's a lifesaver when you want to get to a backup copy of something. Figure I would pass it on.
#4: Is there a site or a place where a person can go to have their website evaluated by real people and find bugs that maybe didn't show up during the individual testing?

Thank You

pidd
Posts: 4388
Joined: Fri May 29, 2020 8:29 pm
Location: Wirral, UK

Re: Needing Some PHP Guidance

Sun Dec 04, 2022 12:24 pm

timlab55 wrote:
Sun Dec 04, 2022 12:28 am
This post is now closed. I reworked the problem and it is now solved. Again, I would like to thank all of you who help with this. I'm in the testing phase of my project now. There are a few things I do need to ask tho so please bear with me.

#1: Once a person enters my website and starts to do things and on most pages there are sessions, if that person doesn't log out correctly, do the sessions still go away after they leave?
#2: The same thing with variables, please?
#3: I found an interesting program called DiskIntemals and it's a Linux Reader for Windows. I'm telling you it's a lifesaver when you want to get to a backup copy of something. Figure I would pass it on.
#4: Is there a site or a place where a person can go to have their website evaluated by real people and find bugs that maybe didn't show up during the individual testing?

Thank You
1. Often there is a cron or systemd run that kills off (cleans) old php sessions eventually. Run the following to see them, mine runs every 20 mins

Code: Select all

journalctl|grep php
2. Variables die when the php program dies, it would be unusual (but not impossible) for php code not to have an end point. If you want data to carry over you can use cookies or similar to pass the info back from the client to the server. PHP times out eventually even if you do have an everlasting loop - provided you have changed it to production settings.

3. Best backup system for Pi's is from this forum's RonR called image-backup. For database backup I used to use automysqlbackup which is in the repo.

4. Yes, you can pay to have your code tested but unless you have deep pockets you aren't going to get a guarantee, don't bother if it is not a commercial site (imho).

timlab55
Posts: 264
Joined: Sun Aug 01, 2021 1:52 am

Re: Needing Some PHP Guidance

Sun Dec 04, 2022 2:38 pm

Code: Select all

journalctl|grep php
I saw your post about this command and it show some interesting things about mine. For example:
(root) CMD ([-X /USR/LIB/PHP/SESSIONCLEAN] && IF [! -D /RUN/SYSEMD/SYSTEM ]; THEN /USR/LIB/PHP/SESSION CLEAN; FI) Start Clean PHP session files....
If a person is running your program and this program ran, wouldn't it stop the person from using sessions?
There was one entry that I'm wondering about:
user denied: root (mysql-denied) from 18.185.239.183. Was this someone who tried to break in? The IP address is from: Frankfort Germany.

pidd
Posts: 4388
Joined: Fri May 29, 2020 8:29 pm
Location: Wirral, UK

Re: Needing Some PHP Guidance

Sun Dec 04, 2022 4:41 pm

timlab55 wrote:
Sun Dec 04, 2022 2:38 pm
Code: Select all

journalctl|grep php
I saw your post about this command and it show some interesting things about mine. For example:
(root) CMD ([-X /USR/LIB/PHP/SESSIONCLEAN] && IF [! -D /RUN/SYSEMD/SYSTEM ]; THEN /USR/LIB/PHP/SESSION CLEAN; FI) Start Clean PHP session files....
If a person is running your program and this program ran, wouldn't it stop the person from using sessions?
There was one entry that I'm wondering about:
user denied: root (mysql-denied) from 18.185.239.183. Was this someone who tried to break in? The IP address is from: Frankfort Germany.
sessionclean won't kick anyone out unexpectedly, there is a maxlife setting or similar somewhere..

You may need to read up on sql injection, this is where you have an input on a website that is unfiltered, if someone enters a sql command on the input it can be sent to the sql database and nefarious things can happen.

If you have a website there will be a non-stop barrage of people (and bots) attempting to break your system, it goes with the territory.

timlab55
Posts: 264
Joined: Sun Aug 01, 2021 1:52 am

Re: Needing Some PHP Guidance

Sun Dec 04, 2022 4:50 pm

Yea, I would agree with you on that. I can spend easy 1 to 2 hours a night ufw blocking people. Okay, so where would I put the command to clean sessions or that code you gave me?
Thanks

pidd
Posts: 4388
Joined: Fri May 29, 2020 8:29 pm
Location: Wirral, UK

Re: Needing Some PHP Guidance

Sun Dec 04, 2022 6:47 pm

You already have cleansessions running, the code I gave you shows the cleansession logs.

If you put too many entries in UFW you will slow your server down.

pidd
Posts: 4388
Joined: Fri May 29, 2020 8:29 pm
Location: Wirral, UK

Re: Needing Some PHP Guidance

Sun Dec 04, 2022 8:13 pm

timlab55 wrote:
Sun Dec 04, 2022 2:38 pm
user denied: root (mysql-denied) from 18.185.239.183. Was this someone who tried to break in? The IP address is from: Frankfort Germany.
Thinking about this further, you must have your mysql port (33060, 33062?) forwarded by your router.

Are you accessing your database remotely, if not you should not have those ports forwarded, if your website is on the same machine as the webserver there is no need for external access for mysql, your server talks to it directly locally.

timlab55
Posts: 264
Joined: Sun Aug 01, 2021 1:52 am

Re: Needing Some PHP Guidance

Sun Dec 04, 2022 11:11 pm

I only have two ports open on my router which is 80 and 443. But yea your right about mysqli being on one of those ports. Care to help out? My personal email address is timlab55@aol.com. If you care to share in private, email me on how to stop something like
user denied: root (mysql-denied) from 18.185.239.183. Was this someone who tried to break in? The IP address is from Frankfort Germany.
Thanks

ame
Posts: 6319
Joined: Sat Aug 18, 2012 1:21 am
Location: New Zealand

Re: Needing Some PHP Guidance

Sun Dec 04, 2022 11:17 pm

You don't really want to post your personal email on a public forum. However, it's nice to see Eternal September still lives on.
Hmm. What can I put here?

timlab55
Posts: 264
Joined: Sun Aug 01, 2021 1:52 am

Re: Needing Some PHP Guidance

Sun Dec 04, 2022 11:20 pm

Thanks ame, I'll remember that, but just wondering how I could offer it to him/her. Anyway, who is "Eternal September"?

ame
Posts: 6319
Joined: Sat Aug 18, 2012 1:21 am
Location: New Zealand

Re: Needing Some PHP Guidance

Sun Dec 04, 2022 11:50 pm

timlab55 wrote:
Sun Dec 04, 2022 11:20 pm
Thanks ame, I'll remember that, but just wondering how I could offer it to him/her. Anyway, who is "Eternal September"?
It's an in joke.
Hmm. What can I put here?

Heater
Posts: 19287
Joined: Tue Jul 17, 2012 3:02 pm

Re: Needing Some PHP Guidance

Thu Dec 08, 2022 8:26 am

timlab55 wrote:
Sun Dec 04, 2022 11:11 pm
I only have two ports open on my router which is 80 and 443. But yea your right about mysqli being on one of those ports.
Something is wrong here. Port 80 is for your web server, unsecured HTTP, no encryption. If you web server is open to the public internet and has log in sessions it should not be listening on this port. Close it off.

Port 443 is for you web server, secured with HTTPS. You should be using this if you have people logging in and such like.

Meanwhile, your MySql database is typically listening on port 3306 for database connections. This should not be open to the pubic internet.
user denied: root (mysql-denied) from 18.185.239.183. Was this someone who tried to break in? The IP address is from Frankfort Germany.
This just means people have been trying their luck at logging in to your db. As noted above I strongly suggest you don't expose your db to the public internet.
Slava Ukrayini.

User avatar
rpdom
Posts: 21137
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Needing Some PHP Guidance

Thu Dec 08, 2022 9:11 am

Heater wrote:
Thu Dec 08, 2022 8:26 am
Something is wrong here. Port 80 is for your web server, unsecured HTTP, no encryption. If you web server is open to the public internet and has log in sessions it should not be listening on this port. Close it off.
I leave it open, but set the webserver to redirect all http requests to the https equivalent. I believe that is a valid reason for leaving that port open.
Meanwhile, your MySql database is typically listening on port 3306 for database connections. This should not be open to the pubic internet.
I think the OP may be confusing PHPMyAdmin with the actual database server. It is possible that PHPMyAdmin is accessible via port 80 or even 443, in which case the webserver configuration should be set to deny access to that from external addresses.

If external access to PHPMyAdmin is required it should be done via a VPN connection.
Unreadable squiggle

Return to “Other programming languages”