Go Back   Freethought Forum > The Marketplace > Computers & Technology

Reply
 
Thread Tools Display Modes
  #26  
Old 08-28-2004, 12:30 AM
MinorityReport's Avatar
MinorityReport MinorityReport is offline
Member
 
Join Date: Jul 2004
Posts: XXIV
Default Re: Learning Programming Using php

Quote:
Originally Posted by Corona688
I'm not sure that php would be the best language to START programming with. Correct me if I'm wrong, but isn't php completely tied to web servers? That means when something goes wrong it's bitching hard to debug. Perl at least, you can drop to command prompt and run the script directly when the server starts telling you 'the server has had an internal error', most useless error message ever :fuming:

...OK, not quite the most useless. The most useless is 'Error: No error', or maybye 'Not enough memory to completely display the'.
PHP runs from the command line, too, but it's mostly used as a module in a web server such as Apache. You can use PHP to do just about anything you'd use Perl for, although the different languages have their strengths and weaknesses.

I have to admit that I don't understand vm's problem, but this may be because I've been a programmer now for twice as long as I wasn't a programmer (and most of that previous time was spent acquiring the skills I needed to start being a programmer).

Here's an example of some tiny PHP programming that I did with another PHP-based forum package, PHPBB, in order to make it impossible for non-members to snoop. I use the standard "example.com" domain in my examples, as per RFC 2606. This is not a real domain owned by anyone, it's specifically designated for use in examples, so nobody is inconvenienced if someone mistakenly clicks on a link or types these URLs into a browser.

The problem:
  • I wanted to make it impossible for a person who hadn't logged in to do anything but log in. So nobody who didn't come to the site with a valid username and password could possibly find out what was going on.
  • Normally, PHPBB permits people who aren't logged in (guests) to see the list of users, to register, and so on. It's possible to forbid guests to see posts, but they can still snoop around and find out a bit about who is using the forum. They may go through all the trouble of filling out the registration form, only to find that they won't be allowed to join.

Formulating a solution

So I formulated what I wanted to do as a bit of logic, which expressed in pseudocode or structured english is:

Code:
if user is a guest then
   Redirect to login page
endif
If I stick this in front of every web page that I don't want guests to see, I reasoned, this would do the job.

Making the solution work

So now I needed to find out five things:
  • How to find out whether the current user is logged in. If not, he's a guest.
  • How to make PHP cause a redirection from one page to another (this means that if you typed the url of one page into the browser, the server would tell it to load another page instead).
  • The URL of the login page.
  • The PHP files containing all the pages that I want to redirect to the login page
  • Where in each PHP file to stick my "test and redirect" code.

I poked around in the code looking for likely bits, until I found that the way to test to see if a user was logged in (in PHPBB, though not in other PHP-based forum packages, which are all different) was:

Code:
if  (!$userdata['session_logged_in'])
{
    // User is not logged in if we get here
}
And the way to redirect in PHP is to use the header() function to send a HTTP 'Location:' header to the browser. For example:

Code:
header('Location: http://www.example.com/somewheretogo.php');
The URL of the login page for any PHPBB website is of the form:

Code:
http://www.example.com/exampleforum/login.php
I found the URLs of all the files in question by snooping my site and finding places I could get in that I thought a guest shouldn't, and noting the URL. The filename was always the final part of the URL (eg: register.php).

The code fragment to be added was thus something like:

Code:
if  (!$userdata['session_logged_in'])
{
    // User is not logged in if we get here
    header('Location: http://www.example.com/exampleforum/login.php');
}
Where to put it? Well as early as possible, obviously. Well before any of the web page was constructed--because you cannot send HTTP headers in this way after parts of the web page have been sent to the browser. But it must be after the $userdata array was populated, otherwise testing $userdata['session_logged_in'] would not give a valid result.

Fortunately in most of the PHP files I wanted to edit, there was a section that went:

Code:
init_userprefs($userdata);
On investigating my expectation that the init_userprefs() function populates the $userdata array was confirmed. By a process that I won't go into here, this code looks to see if there's a certain cookie on the user's browser, and if so it retrieves certain items of data from its database concerning the session. If there is no cookie, the user must not be logged in.

So the next (and final) thing to do was to insert my code fragment after the init_userprefs() call in each of the files memberlist.php, register.php and so on. The result was perfect. Clicking on the standard navigations (Search, memberlist, register, etc) at the head of the PHPBB page when not logged in always took the user back to the login page.

Confession

Please bear in mind that I have compressed several hours work here, and have omitted the many blind alleys, the wrong guesses, the failures, and the cycles of testing that went into producing a working solution. Here I'm trying to illustrate how it's possible to combine a rudimentary understanding of PHP with a basic understanding of HTTP and a willingness to scan the source code to find out how it functions and how it can be modified to produce a desired result.

Last edited by MinorityReport; 08-28-2004 at 12:50 AM.
Reply With Quote
  #27  
Old 08-28-2004, 03:08 AM
viscousmemories's Avatar
viscousmemories viscousmemories is offline
Admin
 
Join Date: Apr 2004
Location: Ypsilanti, Mi
Gender: Male
Posts: XXXDCCXLVII
Blog Entries: 1
Images: 9
Default Re: Learning Programming Using php

Wow. That's a really great post, Minority Report. Having installed a number of hacks (including a phpBB hack that does almost exactly what you describe) much of it was familiar to me. But seeing the whole process laid out like that makes the logic much more clear to me. Plus there are a couple parts I just wouldn't have thought of, like making sure the userdata array is populated before inserting that code snippet. This post will definitely help me along the road to being a better hacker. Thanks. :yup:
Reply With Quote
  #28  
Old 08-28-2004, 12:50 PM
MinorityReport's Avatar
MinorityReport MinorityReport is offline
Member
 
Join Date: Jul 2004
Posts: XXIV
Default Re: Learning Programming Using php

Yes, I think the methods are easy enough to learn but are not necessarily taught in coding courses that concentrate on introducing a programming language. The basic tool is analytical decomposition. I had a vague idea of what I needed to do early on, and to turn that into a solution I had to break it down into those five small investigations I list at the beginning of the "Making it work" section.

You become good at this by experience. For instance, write a basic "Hello world" page and then find out how to alter it to permit the user to then enter his name, and greet the person by name. This breaks down into three parts:
  • Produce a HTML page that permits a user to enter his name and send it to the server using either the HTTP GET or HTTP POST method.
  • Produce PHP that detects when a user has entered a name via the appropriate HTTP method.
  • Embed PHP to write the name into the HTML page as part of the greeting.

So even at the most basic level it pays to decompose a task into smaller ones, and the skills learned in this exercise would be applicable to nearly every piece of PHP code you are likely to write in future.

Incidentally I do agree that PHP is a more generally useful language than most. Unless you intend to specialize in large scale, high reliability server applications or desktop applications written in C++ or C, the compiled languages are less suitable for development because of their very low-level nature. Scripting languages such as Perl, PHP and even ASP provide most of the linguistic richness you get from these languages, but also come with a much more complete set of practical high level constructs. Provided you can live with the relatively brittle nature of scripted applications, they're more suitable for performiing practical day-to-day tasks.

Last edited by MinorityReport; 08-29-2004 at 01:37 AM.
Reply With Quote
  #29  
Old 08-31-2004, 10:46 PM
ceptimus's Avatar
ceptimus ceptimus is offline
puzzler
 
Join Date: Aug 2004
Location: UK
Posts: XVMMDCCCXX
Images: 28
Default Re: Learning Programming Using php

Problem with learning PHP as your first computer language, is that it is mostly used to produce HTML so you need to know HTML too. Also PHP usually works in harness with a database (often MySQL) and you'll need to learn that as well.

The basics of the PHP language is almost exactly the same as C (which is almost exactly the same as java) except for the variables. Variables in PHP are the same as the ones in Perl.

But the main thing with learning a computer language is motivation. If you are motivated to learn PHP, then that is the one for you. Once you learn any computer language, you'll find learning the next and subsequent ones easier.

My advice is, if you don't know any HTML, learn a bit of that first - this could be seen as the first rung on the ladder of learning PHP.

A good reference is the 'Bible' book - "PHP5 and MySQL Bible" or one of the other editions.
Reply With Quote
  #30  
Old 08-31-2004, 11:21 PM
viscousmemories's Avatar
viscousmemories viscousmemories is offline
Admin
 
Join Date: Apr 2004
Location: Ypsilanti, Mi
Gender: Male
Posts: XXXDCCXLVII
Blog Entries: 1
Images: 9
Default Re: Learning Programming Using php

Quote:
Originally Posted by ceptimus
Problem with learning PHP as your first computer language, is that it is mostly used to produce HTML so you need to know HTML too. Also PHP usually works in harness with a database (often MySQL) and you'll need to learn that as well.
Well fortunately I know HTML very well, so that's not an issue. I don't know much about databases, but as much as was necessary to set up phpBB and vBulletin, plus use phpmyadmin to mess around with the data.

Quote:
The basics of the PHP language is almost exactly the same as C (which is almost exactly the same as java) except for the variables. Variables in PHP are the same as the ones in Perl.
Interesting. I didn't know that.

Quote:
But the main thing with learning a computer language is motivation. If you are motivated to learn PHP, then that is the one for you. Once you learn any computer language, you'll find learning the next and subsequent ones easier.
To me that's the key. I am definitely most motivated to learn php because of this site and other forum sites I've worked on and/or will work on in.

Quote:
My advice is, if you don't know any HTML, learn a bit of that first - this could be seen as the first rung on the ladder of learning PHP.

A good reference is the 'Bible' book - "PHP5 and MySQL Bible" or one of the other editions.
I'll look for those when I'm able to get a book. Any good online resources you know of? I'm already aware of php.net, phpfreaks, and a couple others...
Reply With Quote
  #31  
Old 09-03-2004, 05:24 AM
livius drusus's Avatar
livius drusus livius drusus is offline
Admin of THIEVES and SLUGABEDS
 
Join Date: Apr 2004
Posts: LVCCCLXXII
Images: 5
Default Re: Learning Programming Using php

Quote:
Originally Posted by livius drusus
955 to be precise, gentlemen. I make no guarantees that the number will remain constant.
Did I say 955? I meant 968. Sorry. Typo. :giggle:
Reply With Quote
  #32  
Old 09-06-2004, 10:29 PM
viscousmemories's Avatar
viscousmemories viscousmemories is offline
Admin
 
Join Date: Apr 2004
Location: Ypsilanti, Mi
Gender: Male
Posts: XXXDCCXLVII
Blog Entries: 1
Images: 9
Default Re: Learning Programming Using php

Quote:
Originally Posted by MinorityReport
Yes, I think the methods are easy enough to learn but are not necessarily taught in coding courses that concentrate on introducing a programming language. The basic tool is analytical decomposition. I had a vague idea of what I needed to do early on, and to turn that into a solution I had to break it down into those five small investigations I list at the beginning of the "Making it work" section.

You become good at this by experience. For instance, write a basic "Hello world" page and then find out how to alter it to permit the user to then enter his name, and greet the person by name. This breaks down into three parts:
  • Produce a HTML page that permits a user to enter his name and send it to the server using either the HTTP GET or HTTP POST method.
  • Produce PHP that detects when a user has entered a name via the appropriate HTTP method.
  • Embed PHP to write the name into the HTML page as part of the greeting.

So even at the most basic level it pays to decompose a task into smaller ones, and the skills learned in this exercise would be applicable to nearly every piece of PHP code you are likely to write in future.

Incidentally I do agree that PHP is a more generally useful language than most. Unless you intend to specialize in large scale, high reliability server applications or desktop applications written in C++ or C, the compiled languages are less suitable for development because of their very low-level nature. Scripting languages such as Perl, PHP and even ASP provide most of the linguistic richness you get from these languages, but also come with a much more complete set of practical high level constructs. Provided you can live with the relatively brittle nature of scripted applications, they're more suitable for performiing practical day-to-day tasks.
Btw, this was another really helpful post Minority Report. It just occurred to me that I never said as much despite thinking it. Thanks. :)
Reply With Quote
  #33  
Old 09-15-2004, 03:05 AM
EvilYeti's Avatar
EvilYeti EvilYeti is offline
Member
 
Join Date: Sep 2004
Posts: III
Default Re: Learning Programming Using php

Quote:
Originally Posted by viscousmemories
So for you computer programmers out there (and I know you're out there) can you tell me what to do? I've already looked at the local college course catalog and they offer only perl, javascript and C++ classes. I could buy a For Dummies or Sam's teach yourself Rocket Science in 15 minutes book, but something tells me that won't carry me much further (farther?).
Well, first of all, do you have a apache/php you can play on without worrying about breaking anything important? The best way to learn programming is by doing, especially doing dangerous things. And personally, I feel php is a fine language to learn to program with. It's higher level then C and sports a much cleaner design that perl.

While I don't know much about your background, I would say the real problem you are facing is that you don't have a solid grasp of the underlying theory of programming. That and you are probably also going to encounter what I call "minutae overload", i.e. dozens of annoying little technical details that will get in the way of whatever you are trying to do. All I can say about the latter is have patience, persistence and google.

Regarding the former, well thats a tough one. People go to school for four years to learn software engineering skills; its a process that you cannot really shortcut. A good way to start is to check out the language reference for php @ http://www.php.net/manual/en/langref.php . Go through each line item one at a time and look it up if you don't understand what it means. It's helpful here if you have a friend with a comp. sci. education to help you out.

For example, consider "types". If you don't know what that means relative to php, figure it out before continuing. Once you understand that, look through the types available to php (e.g. boolean, integers, floats, strings, etc) and make sure you understand what each one of those means. And I mean really understand, i.e. you see where each would be applicable is solving a problem in the real world.

Once you have a solid understanding of the programming contructs available to you, try writing out the some pseudocode or flowcharts for a simple program. Once you have the basic design down, try writing little bits of it and getting them to work first. Its imperative that you figure out how to see errors from php, to help you debug your programs. A good way to do this is use the command-line interpreter first. See if you can find a guru that can help you get past any road blocks you encounter. Once you get one fragment working, start on the next. Use the php.net to research functions and language features and particluar attention to the comments, they are very helpful.

Now that I think about it, probably what you want is an into to computer science/programming book or course. Unfortunately I'm not aware of any that specifically use php, but the language really doesn't matter. I hear some colleges are using python, which is another great langugage, so if you can find something like that it will serve you well.

Also check out the php entry @wikipedia, http://en.wikipedia.org/wiki/Php
... especially the tutorials section.
Reply With Quote
  #34  
Old 09-15-2004, 05:11 AM
viscousmemories's Avatar
viscousmemories viscousmemories is offline
Admin
 
Join Date: Apr 2004
Location: Ypsilanti, Mi
Gender: Male
Posts: XXXDCCXLVII
Blog Entries: 1
Images: 9
Default Re: Learning Programming Using php

Quote:
Originally Posted by EvilYeti
Well, first of all, do you have a apache/php you can play on without worrying about breaking anything important? The best way to learn programming is by doing, especially doing dangerous things. And personally, I feel php is a fine language to learn to program with. It's higher level then C and sports a much cleaner design that perl.
I do. I have a 'nix server at home with a web server set up, plus this site is hosted on a vps with plenty of room, so I have a live test environment too. Did you see this thread about the smilie project we're working on? So far JoeP is doing the bulk of the coding, but he's annotating the code nicely so I can get a feel for what does what. I've already learned a lot.

Quote:
While I don't know much about your background, I would say the real problem you are facing is that you don't have a solid grasp of the underlying theory of programming. That and you are probably also going to encounter what I call "minutae overload", i.e. dozens of annoying little technical details that will get in the way of whatever you are trying to do. All I can say about the latter is have patience, persistence and google.
You're right that I don't have any background in programming theory. I worked as a Novell network engineer for about 8 years so I have a lot of hands on computing experience, but nothing really focused on programming. I don't have patience but I do have obsessive (er... persistent) and Google. ;)

Quote:
Regarding the former, well thats a tough one. People go to school for four years to learn software engineering skills; its a process that you cannot really shortcut. A good way to start is to check out the language reference for php @ http://www.php.net/manual/en/langref.php . Go through each line item one at a time and look it up if you don't understand what it means. It's helpful here if you have a friend with a comp. sci. education to help you out.
Reading the language reference is an excellent idea. I'll start on that right away. Since I'm working with php here with vBulletin I started by reading the vBulletin code standards. I actually found a bug in the logic of one of their simple examples and submitted a Bug report (now marked fixed and closed) at their website. I was pretty pleased with myself for that one.

Quote:
For example, consider "types". If you don't know what that means relative to php, figure it out before continuing. Once you understand that, look through the types available to php (e.g. boolean, integers, floats, strings, etc) and make sure you understand what each one of those means. And I mean really understand, i.e. you see where each would be applicable is solving a problem in the real world.
Roger.

Quote:
Once you have a solid understanding of the programming contructs available to you, try writing out the some pseudocode or flowcharts for a simple program. Once you have the basic design down, try writing little bits of it and getting them to work first. Its imperative that you figure out how to see errors from php, to help you debug your programs. A good way to do this is use the command-line interpreter first. See if you can find a guru that can help you get past any road blocks you encounter. Once you get one fragment working, start on the next. Use the php.net to research functions and language features and particluar attention to the comments, they are very helpful.
I didn't even know there was a command-line interpreter for php...

Quote:
Now that I think about it, probably what you want is an into to computer science/programming book or course. Unfortunately I'm not aware of any that specifically use php, but the language really doesn't matter. I hear some colleges are using python, which is another great langugage, so if you can find something like that it will serve you well.
Well a book and/or course are unfortunately out of the question at the moment, as I'm broke. But I think you're right. As soon as I can I'll get something. Actually my housemate might have something like that. I'll check.

Quote:
Also check out the php entry @wikipedia, http://en.wikipedia.org/wiki/Php
... especially the tutorials section.
Cool, I love wikipedia but I hadn't thought to look there for php info. Thanks. :)

Oh, and :welcome: to the FF. Good to see ya here.
Reply With Quote
  #35  
Old 09-16-2004, 07:13 AM
seebs seebs is offline
God Made Me A Skeptic
 
Join Date: Jul 2004
Location: Minnesota
Posts: VMMCMXCV
Images: 1
Default Re: Learning Programming Using php

Hi. I'm a programmer, and I didn't ever get taught. I got lucky; my parents never told me I couldn't program, so I just did it anyway.

http://www.plethora.net/~seebs/c/growup.html

Dennis Ritchie once told me that he thought I was the only other person he'd ever met who never had to learn C.
__________________
Hear me / and if I close my mind in fear / please pry it open
See me / and if my face becomes sincere / beware
Hold me / and when I start to come undone / stitch me together
Save me / and when you see me strut / remind me of what left this outlaw torn
Reply With Quote
  #36  
Old 10-02-2004, 08:00 PM
seebs seebs is offline
God Made Me A Skeptic
 
Join Date: Jul 2004
Location: Minnesota
Posts: VMMCMXCV
Images: 1
Default Re: Learning Programming Using php

Ironically, I already know programming, and now I have a need to learn PHP very quickly. I'm setting up a web page for the studio my wife is in, and I want to use PHP for some stuff, because it will reduce the hassle of doing CGI scripts. (Suexec doesn't scale well to my context.)
__________________
Hear me / and if I close my mind in fear / please pry it open
See me / and if my face becomes sincere / beware
Hold me / and when I start to come undone / stitch me together
Save me / and when you see me strut / remind me of what left this outlaw torn
Reply With Quote
  #37  
Old 10-02-2004, 09:27 PM
JoeP's Avatar
JoeP JoeP is offline
Solipsist
 
Join Date: Jul 2004
Location: Kolmannessa kerroksessa
Gender: Male
Posts: XXXVMMLXXXV
Images: 18
Default Re: Learning Programming Using php

You'll find it easy enough, using www.php.net/manual, and if you like tutorials, the links vm's posted above.

... vm, are you ready for another project?
__________________

:roadrun:
Free thought! Please take one!

:unitedkingdom:   :southafrica:   :unitedkingdom::finland:   :finland:
Reply With Quote
  #38  
Old 10-02-2004, 09:48 PM
viscousmemories's Avatar
viscousmemories viscousmemories is offline
Admin
 
Join Date: Apr 2004
Location: Ypsilanti, Mi
Gender: Male
Posts: XXXDCCXLVII
Blog Entries: 1
Images: 9
Default Re: Learning Programming Using php

Quote:
Originally Posted by JoeP
You'll find it easy enough, using www.php.net/manual, and if you like tutorials, the links vm's posted above.

... vm, are you ready for another project?
Sure thing. Watcha got on your mind?
Reply With Quote
  #39  
Old 10-02-2004, 11:28 PM
seebs seebs is offline
God Made Me A Skeptic
 
Join Date: Jul 2004
Location: Minnesota
Posts: VMMCMXCV
Images: 1
Default Re: Learning Programming Using php

What's actually kicking my ass so far is that I really want Apache to run virtual servers ENTIRELY as the specified user; I want PHP scripts and everything to run as that user, not just suexec for CGI scripts. But this cannot be.

More subtly, I had the idea of having www.example.com/user go to ~user, but this breaks suexec; it wants www.example.com/user to be run as the ID associated with the virtual host, and I can't say "but this path gets special treatment". Waah!

But I'm making progress, by accepting that PHP scripts will need group privs to scribble on databases.

One last thing to figure out, which is PHP scripts trying to get at a PostgreSQL database hanging indefinitely, and I can have the stuff I promised the girls up and running.
__________________
Hear me / and if I close my mind in fear / please pry it open
See me / and if my face becomes sincere / beware
Hold me / and when I start to come undone / stitch me together
Save me / and when you see me strut / remind me of what left this outlaw torn
Reply With Quote
  #40  
Old 10-02-2004, 11:43 PM
JoeP's Avatar
JoeP JoeP is offline
Solipsist
 
Join Date: Jul 2004
Location: Kolmannessa kerroksessa
Gender: Male
Posts: XXXVMMLXXXV
Images: 18
Default Re: Learning Programming Using php

Quote:
Originally Posted by viscousmemories
Quote:
Originally Posted by JoeP
... vm, are you ready for another project?
Sure thing. Watcha got on your mind?
One you do by yourself (but on the test system so I can see your code and template), calling for help in the form of answers in English rather than in code - you know what I mean here - show how not do it for you. *Various proverbs and witticisms apply here.*

I was thinking of something probably trivially simple - make a panel to edit multiple smilie keywords at once. (And then package up this feature as something for the wider vb community.) Do we discuss this here or at the test forum?
__________________

:roadrun:
Free thought! Please take one!

:unitedkingdom:   :southafrica:   :unitedkingdom::finland:   :finland:
Reply With Quote
  #41  
Old 10-03-2004, 12:13 AM
viscousmemories's Avatar
viscousmemories viscousmemories is offline
Admin
 
Join Date: Apr 2004
Location: Ypsilanti, Mi
Gender: Male
Posts: XXXDCCXLVII
Blog Entries: 1
Images: 9
Default Re: Learning Programming Using php

Sounds good. Let's discuss at the test forum. :)
Reply With Quote
Reply

  Freethought Forum > The Marketplace > Computers & Technology


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

 

All times are GMT +1. The time now is 04:30 AM.


Powered by vBulletin® Version 3.8.2
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Page generated in 0.56143 seconds with 15 queries