Notices
Computer & Technology Related Post here for help and discussion of computing and related technology. Internet, TVs, phones, consoles, computers, tablets and any other gadgets.

I need some help with PHP, SQL and HTML

Thread Tools
 
Search this Thread
 
Old 05 March 2009, 04:38 PM
  #1  
Markus
Scooby Regular
Thread Starter
 
Markus's Avatar
 
Join Date: Mar 1999
Location: The Great White North
Posts: 25,080
Likes: 0
Received 0 Likes on 0 Posts
Default I need some help with PHP, SQL and HTML

Afternoon all,
I'm attempting to learn a bit more about PHP, SQL (specifically mySQL) and HTML.

I know a bit about HTML, as I've coded websites. I know what PHP is, and the same is true of SQL, but I'm only just delving into how to use them.

I've looked at some examples and have worked out how to write PHP code to create a table in a mysql database and also how create a record in that table and also how to read the contents of that record back.

It all seems somewhat straightforward, which is good. So I'm plodding along and trying various things out.

So what is it that I need help with? Ok, here goes (I think we're probably talking about running very very quickly before learning how to use ones legs ):

I want to try and re-write a login system in PHP. The current setup uses a cgi, which is a Mac application, written for pre OS X systems. Thus I can't just port things over to OS X or any *nix system.

The way the system currently works is that someone submits a form which sends an email with their username and email address (and possibly other bits of information) and these are manually checked to make sure the person is real and it's not some bot.

Using this information, I run a script which has an interface that allows me to input this information, plus it allows me to generate a password, and set various options, which are boolean in nature (has access to ABC, has access to XYZ, etc...). This will then create a file in a folder. The name of the file is the user's name, and the contents of the file are the email address, password, and the other boolean options. An email is then sent to the user with their login information.

They will then go to a page on a website, which calls the cgi, this prompts for the name and password, and it will then scan the folder structure to see if there is a file with the name entered, if a match is found, it reads the file, grabs the password, checks that against the input and if it's valid the user is logged in and a new page is displayed.

The cgi is also being clever about this page. It's reading the page (it's got a different extension, and the server's mime type has been configured to use the cgi if a page with this extension is loaded) and looking for various tags in the file, for example:

Code:
«StartShow ABC»
Show this data if the user has ABC access
«EndShow ABC»

«StartShow XYZ»
Show this data if the user has ABC access
«EndShow XYZ»
The cgi compares the values it's read from the file on login against these tags. Thus if the "has access to ABC" value is true (actually it's 1 or 0, so in this case if it's 1) then the html data within that tag is displayed, and if the value is false, the data is not displayed.

Phew. Still with me?

So far I've got my database setup, I can add users to it, and read them back, and I'm just reading about how to create login sessions and it seems that this side of things should be pretty easy. The bit I'm wondering about is the second portion, the reading of the html like file (it is just HTML but with a different extension and those special tags).

I know I can perform conditional checking in the php section of the file, so I can get the "has access to ABC" value from the database and check if it's 1 or 0. However, I'm trying to figure out if I'd need to have HTML code within the PHP section of the page, or wether I can have it after that?

I've messed around a little with vBulletin, and know that they have, in HTML, conditional checking lines such as:

Code:
	<if condition="$show['pmlink']">
<tr><td class="vbmenu_option"><a href="private.php?$session[sessionurl]do=newpm&amp;u=$post[userid]" rel="nofollow"><phrase 1="$post[username]">$vbphrase[send_private_message_to_x]</phrase></a></td></tr>
	</if>
I'm unsure as to wether this is something special that vB handles, as in it's got some class or something somewhere that knows what to do when it comes across the <if condition> lines in html. To me it seems that this would be perfect, as I could do a search and replace in the current files for the «StartShow ABC» and «EndShow ABC» lines and replace them with the conditional if statements.

Hmm, thinking about it, I guess I could just do that anyway, replacing them with php "if" statements and moving the stuff inside a php block.

Any advice or suggestions greatly welcomed.

Last edited by Markus; 05 March 2009 at 04:40 PM.
Old 05 March 2009, 10:14 PM
  #2  
RichB
Scooby Regular
 
RichB's Avatar
 
Join Date: Apr 1999
Location: Bore Knee Muff
Posts: 3,666
Likes: 0
Received 0 Likes on 0 Posts
Default

Not had time to digest all that Marcus but phpmyadmin runs under MAMP (or any php/mysql installation) and is very good.

You can run queries, create databases and tables and it will show you the php code to do it...
Old 06 March 2009, 03:11 AM
  #3  
Markus
Scooby Regular
Thread Starter
 
Markus's Avatar
 
Join Date: Mar 1999
Location: The Great White North
Posts: 25,080
Likes: 0
Received 0 Likes on 0 Posts
Default

Hi Rich,
One step ahead of you I've had phpmyadmin installed for a while, but only ever used it to export and import databases. I've basically done what you suggest and watched the commands that get issued to create/modify/delete tables/records, and am learning how to put those commands into php.

I've setup a test page, replacing the «StartShow» tags with:

Code:
<?php if ($ShowABC == "1") {

print "<p>This is displayed if I have ABC access</p><br>";
}

?>
It does work, but I'm not completely happy about using the print command with multiple lines of html, as I'll have to start escaping characters.
Old 06 March 2009, 08:35 AM
  #4  
RichB
Scooby Regular
 
RichB's Avatar
 
Join Date: Apr 1999
Location: Bore Knee Muff
Posts: 3,666
Likes: 0
Received 0 Likes on 0 Posts
Default

You end the php tag and then follow on with html, then just open a php tag again and close the braces.

<?php if ($ShowABC=="1") { ?>
<p>blah</p>
<h1>a title</h1>
<img src="" title="" />
<?php } ?>
Old 06 March 2009, 08:39 AM
  #5  
RichB
Scooby Regular
 
RichB's Avatar
 
Join Date: Apr 1999
Location: Bore Knee Muff
Posts: 3,666
Likes: 0
Received 0 Likes on 0 Posts
Default

I always use
echo 'some text';
as well, not sure which is best to be honest lol.
If you use double quotes you can use variables within the string ala
echo "print this $variable and then $another variable";
where as with single quotes you would do
echo 'print this'.$variable.' and then '.$another.' variable';

Also you could pout common html into there own php files and include them.
include 'commonhtml.php';

Better still start looking into object based programming and use methods...
I build all our sites in php/mysql...
Old 06 March 2009, 11:53 AM
  #6  
Markus
Scooby Regular
Thread Starter
 
Markus's Avatar
 
Join Date: Mar 1999
Location: The Great White North
Posts: 25,080
Likes: 0
Received 0 Likes on 0 Posts
Default

Originally Posted by RichB
You end the php tag and then follow on with html, then just open a php tag again and close the braces.

<?php if ($ShowABC=="1") { ?>
<p>blah</p>
<h1>a title</h1>
<img src="" title="" />
<?php } ?>

Ah ha! That makes sense and is exactly what I'd want to do, so I'll give that a shot.

Originally Posted by RichB
I always use
echo 'some text';
as well, not sure which is best to be honest lol.
If you use double quotes you can use variables within the string ala
echo "print this $variable and then $another variable";
where as with single quotes you would do
echo 'print this'.$variable.' and then '.$another.' variable';

Also you could pout common html into there own php files and include them.
include 'commonhtml.php';

Better still start looking into object based programming and use methods...
I build all our sites in php/mysql...
At present I've got a couple of files and they've got all the php code in them ,but I've used the include command when designing a previous site and so will move the common code into a file which I'll include.

I was considering converting the whole site to something like Joomla, that does have a login system, however, their ACL stuff is very limited, unless I pay for the, umm, think it's BEYOS jACL product, which then, I think, as it's been a while since I used it, the ability to define my own groups, at which point I could give the level of access I want.

Unless I'm missing something and it is possible to configure multiple groups and have users present in multiple groups and set viewing permissions based on those group assignments.
Old 06 March 2009, 03:51 PM
  #7  
Markus
Scooby Regular
Thread Starter
 
Markus's Avatar
 
Join Date: Mar 1999
Location: The Great White North
Posts: 25,080
Likes: 0
Received 0 Likes on 0 Posts
Default

Roight, I've been playing around and the php tagging you mentioned is working perfectly Thank you very much indeed Rich
Old 06 March 2009, 09:09 PM
  #8  
Markus
Scooby Regular
Thread Starter
 
Markus's Avatar
 
Join Date: Mar 1999
Location: The Great White North
Posts: 25,080
Likes: 0
Received 0 Likes on 0 Posts
Default

Ok, another bozo moment here.
I want to compare two items, they will be text strings and one will, eventually, be the input from a form.

It's basically a username and password submittal form. I want the user name to be case insensitive, but the password, obviously to be case sensitive.

I *think* the password bit is easy, because I can go

$user_input = $the_password

and if it's a match then that's good.

It's when it comes to the username that I'm having problems, as I want it to be case insensitive.

Any pointers?
Old 06 March 2009, 10:53 PM
  #9  
john_s
Scooby Regular
iTrader: (1)
 
john_s's Avatar
 
Join Date: Dec 2002
Location: Preston, Lancs.
Posts: 2,977
Likes: 0
Received 0 Likes on 0 Posts
Default

Does PHP have a toupper or similar function?

Convert both to upper (or lower if you like) case and then compare them.
Old 06 March 2009, 11:10 PM
  #10  
RichB
Scooby Regular
 
RichB's Avatar
 
Join Date: Apr 1999
Location: Bore Knee Muff
Posts: 3,666
Likes: 0
Received 0 Likes on 0 Posts
Default

john's right, convert them both toupper() or tolower()
uk.php.net is your friend!
PHP: strtoupper - Manual
PHP: strtolower - Manual

Also, dont forget if $user_input = "abc" and $the_password="def" after you do if($user_input = $the_password) echo 'blah';
then $user_input will be "def"

You must use == with IF statements unlike asp
Old 07 March 2009, 12:46 AM
  #11  
Markus
Scooby Regular
Thread Starter
 
Markus's Avatar
 
Join Date: Mar 1999
Location: The Great White North
Posts: 25,080
Likes: 0
Received 0 Likes on 0 Posts
Default

Thanks for that, I'll try the upper or lower case thing and give it a shot.
Old 07 March 2009, 03:02 AM
  #12  
Markus
Scooby Regular
Thread Starter
 
Markus's Avatar
 
Join Date: Mar 1999
Location: The Great White North
Posts: 25,080
Likes: 0
Received 0 Likes on 0 Posts
Default

It worked brilliantly. Thank you both

I've now got a form that checks username and password, makes sure mandatory fields are filled out and also checks people are not putting URL's where they should not be, and it also removes funny characters from fields.
Related Topics
Thread
Thread Starter
Forum
Replies
Last Post
shorty87
Full Cars Breaking For Spares
19
22 December 2015 11:59 AM
LSherratt
Non Scooby Related
20
28 September 2015 12:04 AM



Quick Reply: I need some help with PHP, SQL and HTML



All times are GMT +1. The time now is 01:08 AM.