Editing PHP Pages...In PHP - php

This is quite a complex question for what is a complex idea and project. I'm wondering if there is a way to edit certain lines of a PHP page through a PHP page? One page, lets say (index.php) would be for the public whilst another (edit.php) would be the page to edit certain things on this page. I'm looking for a way to update the data, not just replace the file.
The edit.php file would show what is currently there and allow the user to change it and the save it. Is this possible? I can already see replies saying "Why are you not using a Database?"... It's easier this way, I think? Any suggestions are appreciated.

My recommendation would be to create an object that represents the information they can edit and then use PHPs serialize and unserialize to map it to a string and back.
The string can be stored in a text file and PHP handles changes to the object very gracefully if you add new properties.
If you want them to actually change PHP code, it could be very volatile.

Related

Do php variables persist (using AJAX)?

This is my first question on this site, so I'm going to try to be as specific as i can... By the way, excuse me for my perfect knowledge of English... which isn't perfect at all..
So what I was wondering is: do php variables persist if you change your page's content, using AJAX methods?
Let me explain: I want to code a web app which has to contain a main layer, containing a few tabs. The user has to be able to write stuff into text areas in one tab, switch the tab, and if he wants so, come back to the first tab to complete what he wrote before (also the app has to keep the php variables that it created previously right?). The app also has to put all the data, entered in all the different tabs, in one or many databases, when a summit button is clicked; so it has to access all the variables created before.
I just don't have any code at this moment, but I want to do a specification file before starting to code, because what I am about to do is kinda massive app, so i hope you will be able to explain me this point.
I still thank you for your help.
You would be best to consider the PHP script as a one-off thing. It does what it is asked to, then terminates. Nothing is preserved.
However, if you NEED to preserve something to pass back with an AJAX call, you can do it by including:
<INPUT type='hidden' id='my_variable' value='my_value'>
This can be referenced by the javascript that calls your AJAX PHP page and thus be passed back.
For what you require, as #AlexP said, you can simply change the visibility of each tab content area with:
<SPAN onclick='toggle(this.id)' id='tab_1'>Tab Name</SPAN>
or similar. Your JS function might include something like:
for(n=1;n<=numberOfTabs;n++)
{
document.getElementById("div_"+n).style.display="none";
}
document.getElementById("div_"+passedid).style.display="block";
though there are other ways of doing it.
Perhaps what you REALLY want to do is save the entered data into a database field frequently (or even continuously).

How to run external php files and put their output into a string?

Have a abstract how-to question which I haven't found a solution.
Lets say you built a plugin for a CMS like wordpress, I'm using a MCMS called GetSimple.
And now within that plugin.. when a button is clicked by the user... two external php files have their code ran and their output taken and put collectively into a single static file.. say a html or css file.
So then in this kind of scenario... how can you (within the plugin) run an external php file without effecting the current page you are on, then take that output and put it as a string into a variable.. repeat this for another php file... then take the two string outputs, merge them... then put them into a single static file?
This has proven to me to be a very difficult task.
for more details you can see this question:
https://stackoverflow.com/questions/15080163/how-to-create-a-file-with-php
So how would you go about doing this?
I was looking at the possibility of saving each file's output into separate xml files and then merging those xml files... but the problem still remains of running external php and putting that data somewhere without affecting the current page PHP you are on.
If you can generate the response on the server-side, you could simply run those PHP scripts using for example shell_exec() or using Symfony2 Process Component, then gather the results using file() or file_get_contents() functions.
If you need to have this things generated on button click, you must notify server to handle that tasks, and to do so you need to make an AJAX calls calling that scripts using methods I've told you above.

How to organize or use pre-exsisting html into a new php application? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Looking for ideas on how to go about working with a large set of .html files (50,000+) that are preexisting.
What I am trying to accomplish is to be able to keep the same hierarchy, links etc... but add content to them via php or whatever etc..
To be able to organize or route them into a php application or my own cms. I have been thinking ways to do this and come up with a few ideas but hoping to get some feedback from some pros.
I really only need to create something that will get the data once and add it to a cms that I develop. I have a script that I wrote to be able to index and search etc.. but how to go about using the contents and adding to them while keeping structure has me swinging a little. Any thoughts?
UPDATE: I found some tips that made this easier for me as well
Use a .htacess file and ad:
AddType application/x-httpd-php .html .htm
Then I can add includes into those files, I am note sure where to put this in the config file yet but will update when I know.
If you were to use a CMS like Wordpress, assuming your content follows some sort of regular structure, you could recreate the HTML frame as templates and then write a script that parses each HTML file and creates a post based on criteria like the HTML file name.
You can even match the file name by setting the slug on post creation, though you'll need a server rewrite rule to drop the .htm(l) if you intend to keep all the backlinks to the site.
I'm oversimplifying of course, as with 50,000+ files it isn't a trivial job to move all the data.
Perhaps automate it using something like this: http://wordpress.org/extend/plugins/import-html-pages/
The way I would go about it is to keep the html in a function which you require. Separate them logically as:
function output_html_one($link_here, $or_link_there...) {...}
function output_html_two($other_links...) {...}
When you want to output them with the files you have, just call the function, and it will be sent out. I am sure others will give you preferences. This allows you to give them access to variables through arguments, which is a downside, but it keep everything organized and provides some clarity as you can separate the raw html from the logic in your code. I am very curious to see what others say.
EDIT: I added $user inside of html_header so its clearer. For example, lets say you might want to check if a user is logged in. This gives you a way to tailor your html. Then you go to the files which contains this, you can jump in and out of the php tags in order to add dynamic content.

How to see what function a specific button calls in PHP?

I have been given a large PHP project that I am supposed to fix all the problems of.
The code has little to no documentation, and is a real mess organizationally. (Coding was outsourced to several different offshore developers)
Anyways, I am relatively new to PHP, coming from Java.
I'm using netbeans.
My question is: How can I figure out what function is being called for a specific button, and what is the best way to find where that is in the code?
Thanks,
The short answer is, "it depends."
Is it part of a form? Is it an image that is just hyperlinked to another page? The most simple way of finding out is to view the source code of the page and find the HTML code for the button.
If it submits a tag, then look at the form's ACTION attribute for the page that deals with the data. That page will likely retrieve the information using the $_POST or $_GET variables.

Creating webpage on form submit?

How is it possible to allow a user to create a webpage containing some html, based on their entries in a form? ie. I would want them to be able to input a name and when the button is clicked, a webpage called that name would be created. I imagine that this must be possible in php, but what functions/code would I be using?
Thank you!
First of all, I would ask myself if this really what I need.
Why don't you just create a basic template page and pass the data as a parameter?
Something like:
user.php?name=joachim
If you still want to do that, you could use the PHP functions for opening, writing and closing files:
http://php.net/manual/en/function.fopen.php
http://php.net/manual/en/function.fwrite.php
http://php.net/manual/en/function.fclose.php
Here's a tutorial on how to create a file in php:
http://www.tizag.com/phpT/filecreate.php
This can pose big security risks in the first place, anyways, suppose form is submitted with html text, you can go about something like this:
$handle = fopen('file_name.html', 'r+');
// write to file
fwrite($handle, $_POST['fieldname']);
close($handle);
Note: You got to consider XSS and other security issues because you are allowing users to create pages. Bad guys can exploit it easily.
Resoures:
PHP File Handling Functions
XSS
Finally, a must read PHP security guide:
http://phpsec.org/projects/guide/
Idea:
Just allow them minimal of customization and use a pre-made page where you could insert this user data. Ofcourse as said, you need to sanitize the user-submitted data and consider security issues.
I would just have a page that acts as a template that populates based on the fields you mention, unless you actually need a physical file.
The template makes changes and security much easier.
UPDATE: Basically you can either have your form submit to a page that prints those POSTed fields to the appropriate spots or post to a database and then pull to the page.
You'd need to extract the entries of the form from the $_GET or $_POST arrays, and then use file writing functions such as fwrite to create a file. Theres an example on how to do a simple write here.
Excluding the fact that it is a security risk to allow users to create any files on the host...
You can create a file via fopen(), fwrite() and fclose() or using file_put_contents(). I prefer using some kind of template file which you load first, then add the user input values in it and then save the whole string via fwrite() or file_put_contents(). You get the user inputs from the $_GET or $_POST array, depending on the method you used in your form.
By saying "create a webpage containing some html, based on their entries in a form" I'm going to assume ( yeah yeah I know) that what you really mean is to determine the contents of a page by the entries from the form.
Gonna try and explain this as basically as possible, because I'm not completely sure I understand your intent.
When you write the form, you will have an tag that contains action="example.php". This means that it will perform the actions on example.php
In example.php, you can then retrieve the variables passed to it from the form by looking within your $_POST[] array variable. Then, you can change the structure of example.php or your result page by using the variables in php to determine what you print.
I don't think generating a physical file would be necessary, and since you're using a form to get the information in, $_GET is kind of pointless (I'd guess in most cases).

Categories