problem with sync moodle and php archives

problem with sync moodle and php archives

by Carlos Jaimes -
Number of replies: 12
Hi friends, I need some help , I'm working in my grade work , and need to know how can i do for link a php page with my moodle , because I have a aplication , developed in php, and I need to acccess it from a link in moodle and when i do it, I obtain an error that saids that I don't have permision. I need too know how can i do to know the name, and other data from the person that is logged in, need to know which variable have those dates or how can i do to obtain it. My aplication is not a moodle block is an external aplication desarrolled by me. My version of moodle is 1.5.2. Thanks a lot!!
Average of ratings: -
In reply to Carlos Jaimes

Re: problem with sync moodle and php archives

by Shamim Rezaie -

Hi Carlos,

I hope to understand you well. you didn't mention whether moodle shows you the error message or your other php application shows it.

Anyway, you can access the current logged in user by the $USER global object (for example his username is $USER->username).

In reply to Shamim Rezaie

Re: problem with sync moodle and php archives

by Carlos Jaimes -
The problem that I have, is that when I link html page al work correct but when I link a php page (necesary to obtain the user and anythink) the window only shows me the code. like if I open a txt file or document. Thanks for your help bro.
In reply to Carlos Jaimes

Re: problem with sync moodle and php archives

by Chardelle Busch -
Picture of Core developers
Hi Carlos,

My first suggestion would be to upgrade to the latest Moodle version. 1.5 is so old now, that it makes it difficult to get help from developers.

As for your problem, you might try adding:

require_once("../../config.php");

to the top of your file (change the path if necessary)
In reply to Chardelle Busch

Re: problem with sync moodle and php archives

by Carlos Jaimes -
hi chardelle, this config.php is a file inside the Moodle folder or is a file from the php instalation? I need to do that on all the .php files of my aplication?. thanks a lot
In reply to Chardelle Busch

Re: problem with sync moodle and php archives

by Carlos Jaimes -
Here I put an attachment with a file that I want to link and I obtain only the code. I put the require_once("../../config.php"); but I dont know if the place is correct because nothing have changed. Thanks for your help
In reply to Carlos Jaimes

Re: problem with sync moodle and php archives

by Jan Dierckx -

The require_once call is a PHP fucntion. It should be inside php tags. If it's outside PHP tags it will be shown as text. That's why the code is shown, instead of executed. Don't include the HTML header. You should use PHP to send the header. This discussion shows a nice example of a basic Moodle page. It prints the header, checks if the user is loggedin, and provides you with all the global variables you need, like $USER to know which user is logged in.

In reply to Jan Dierckx

Re: problem with sync moodle and php archives

by Carlos Jaimes -
I prove with a page only with the following code

require_once("../config.php");
require_login();

print_header( 'My Page Title',
'My Page Title',
'My Page Title');

print_simple_box_start("center", "", "", 5, "generalbox", "mypage");

print("<p>Hello, world!</p>");

print_simple_box_end("center");
print_footer();

only that code, i erase all the other html stuff, but I still obtain a page with the text , look the pic attachment and you would see what i obtain.
Attachment moodle_error.JPG
In reply to Carlos Jaimes

Re: problem with sync moodle and php archives

by Chardelle Busch -
Picture of Core developers
You need to call the php code directly from a .php file. Maybe look at some of the other Moodle files and see how they are written/used--for example, if you add a resource to any moodle file, you will probably see the same thing.
In reply to Carlos Jaimes

Re: problem with sync moodle and php archives

by Shamim Rezaie -

Hi Carlos,

In order to link your other php file, here is a suggestion when you don't want to modify moodle files:

Choose "Insert a label" from "Add a resource" combo box and then write the text of your link. you may use html tags to put a link instead of a simple label.

Cheers

In reply to Shamim Rezaie

Re: problem with sync moodle and php archives

by Carlos Jaimes -
Hi , tanks for all your help. I install the 1.8 version in otrher computer and now when I try to link the page, the page is displayed but the php code doesn't execute, only appears write in the page. Look the pic attached to see what I got. Somebody now how can I fix that?? I really worrie because I need to present my final work on september 6 th.

If I write the link like you said Shamim, i cannot recieve variables like user name and other that I need.,...
Attachment Error_Moodle.jpg
In reply to Carlos Jaimes

Re: problem with sync moodle and php archives

by Shamim Rezaie -

just write require_once("path_to_your_moodle_root_folder/config.php") and then you can access whatever you need.

Here is an example:

<?php
    require_once('../config.php');
    require_login();
    echo 'Your name is ' . $USER->firstname;
?>

In reply to Carlos Jaimes

Re: problem with sync moodle and php archives

by Jan Dierckx -

From looking at the screenshot, I gather you are still putting your php files inside the moodle data directory. (The location bar shows file.php/2/ before your PHP file. The file.php script doesn't execute PHP code as a security measure. Just put your file somewhere inside your moodle application directory (where the other PHP files reside), NOT the moodle data directory.

Inside the screenshot I also see a lot of direct SQL queries. Please make sure that what you insert into the database ($idpregunta) is completely secure and not vulnerable to SQL injection. It would be better (though I understand you may not have the time) to use Moodle database calls instead, they provide some basic security. It wouldn't be the first time a Moodle installation is made insecure by third party code added to it.

Good luck with your presentation.