To be more specific, add:
require("location/moodle/config.php");at the top of each of your PHP scripts, and then:require_login();followed by your code. All this does is check that the person is logged in to the server - it does not do a check for enrolment in a course (sadly this isn't currently possible in normal resources).
Another way is to use the "Program" resource type. This passes url, username, password etc to the external resource (as part of the URL, ie GET). You can get more details on the format by looking in mod/resource/view.php (must document it some day). The Program type probably needs some work to pass information in a less convoluted way, but it should be usable enough.