How to create dynamic permalink URLs from $_POST data - php

Is it possible to generate nice dynamic/permalink URLs based on $_POST submission form data?
Essentially I'd like the page.php I direct a form to - to create it's own URL from data that's in the $_POST array.
I've only seen methods to create dynamic/permalink URLs from within frameworks or based on SQL database rows/columns.
Whereas, I'd like to go from mywebsite.com/index.php to mywebsite.com/[topic-keyword-from-form-submission]/
A more direct or likely example would be:
mywebsite.com/european-union/
On the index.php page are various category style keywords as buttons within forms - no open text input boxes.
Are there any security concerns/considerations if the above is achievable ?

I would suggest that you start to use mvc. Most framework now are also using MVC. I would also suggest to start using a framework as else you will have to build a routing system each time which takes up time you could have used somewhere else.
I would suggest laravel :)
In shorts it's possible but not worth the time.

Related

CakePHP Dynamically Add new Forms (WYSIWYG)

I've been asked to create a CakePHP plugin that would allow users to dynamically generate forms. This doesn't sound too hard in and of itself (pull fields from DB, figure out what kind of input, show in HTML), but I don't understand how I can "save" the forms to a new page.
From my understanding, Wordpress circumvents this problem by storing all data in the database, and can use shortcodes to just "insert" the form into another page. Seeing that CakePHP handles relies on CTP files to define views (and not Database fields), I don't see any simple approach to doing this.
Is this goal of a WYSIWYG form editor possible in CakePHP (without having to resort to terrible practices like editing CTP/PHP files from within the app)? If so, can you point me towards some strategies I can use to develop this plugin?
I'm not sure what you do with the dynamic form, to me it sounds like you're describing two totally different things.
Save the structure of the generated form somehow in the DB, key/value, serialized via php or as json object
Return the data, set it to the view
Write a helper that turns the structure that describes the form again in
The editable version, restore the form
Parses the structure and returns the HTML of whatever the fields of the form should do
You'll have to write your own parser that looks for things like [gallery id=1] and replace that with the result of a function call. Assuming that this is what you want to do. So you'll have to map the "gallery" to a functional call. I think the best here would be to use requestAction()
The *.ctp files are what WP considers it's template files, and honestly, Wordpress is just a horrible awkward piece of software from a developer perspective.

Dynamically display content based on web address after '/'?

I am wondering if this is possible with PHP, or if not with ASP since it's relatively similar and I could pick it up quickly. Let's say I have a text input for a user to search a database; as an alternative to using the text field I want a user to be able to go to 'www.examplesite.com/SEARCHTERM'. Is this possible? I've seen it done, but I can't figure out how. Thanks!
You might have to look in to url rewriting based on the server you use.
URL Rewritting
This can be done:
1) Using $_GET
www.examplesite.com/?SEARCH=SerachTermHere
In the page you can get the search variable with $_GET['SEARCH']
2) Using URL Rewritting
Yes that's easily possible. For example if you have apache server then you can enable mod rewrite and write one rule to redirect all such requests to your script in a variable and it will then handle accordingly
It is possible, you have two options.
Using URL Rewritting in your server configuration.
Using $_GET global. Everything in URL after your actual page address is available through $_GET.
This goes a bit beyond your question but might prove to be very useful. Even though it is a framework, you might want to look at Symfony 2 routing for ideas how to implement it. Specifically at "Under The Hood" section. You can even explore their routing code on github. Applications made in it have exactly the form you are searching for (content displaying based on $_GET data).

php dynamically create new pages in custom cms

I'm creating my own CMS from scratch as a way to build my php and mysql skills. Everything is going well, but I'm at the point where I want to create individual post pages for each blog post I write. So the index.php page has a list of all my blogs with snippets of each post and there is a read more button that should take the user to the full page for each blog post. Each post has a url created from the blog title entered in the "create post" form. I'm trying to figure out how to create unique pages for each post without passing the title, subhead, post content and other info through the GET.
This also dovetails with another feature I'm trying to add. I want to be able to create individual pages using a "create page" form like I did for my posts. So if I want an "about us" page I go to my admin form, fill out the title, add the content, and when I hit submit it creates the page dynamically. I have thought all day about how I'd do these two things but can't quite figure out how I can do this.
FYI, I'm not asking for code, I just need a push in the right direction as I try to conceptualize how to achieve this. Thanks!
If you're not familiar with the Model-View-Controller pattern, reading up on it might be prudent. MVC is frequently the right starting place for high-level design of web applications.
Also, a CMS is a big enough project you should consider using a PHP framework like CodeIgniter, Symfony, Zend, etc. to make your life easier. It removes a lot of the drudge work and common tasks.
Dynamic Page Creation and Display
I think you want to split it into two things: the text content (basically what you put in the forms) and the HTML templating surrounding that content.
When you make a page or blog post, you would want to store the actual content (what you type into the creation form) in a database of some sort (not necessarily an RDBMS, but if you're trying to build MySQL skills it's a reasonable choice).
Then you would use a separate function to bind that content into an HTML template and present it to the user when they load a given page.
URL Routing
To get nicer-looking URLs you can use something like apache's mod_rewrite. You can use that to convert a URL like this:
posts/how-to-make-a-cms
to this:
posts.php?title=how-to-make-a-cms
Then you can have posts.php read from GET as normal. How you choose to do the conversion is pretty open-ended.
To avoid getting really complicated rewrites, people often just structure everything to go to a central routing script which figures out what class and method to call and what arguments to pass it. So it would rewrite the URL above to:
main.php?a=posts/how-to-make-a-cms
Then main.php would parse out the segments of that argument from GET and figure out where to send them. Like it might take posts/show/how-to-make-a-cms and do something like:
$o = new Posts();
$o->show("how-to-make-a-cms");
If you do it that way, I think you can avoid mod_rewrite entirely as long as you're willing to accept only slightly pretty URLs, like this:
mysite.com/main.php?/posts/show/how-to-make-a-cms
I haven't done this type of thing before (because the frameworks do it so beautifully already), so I might be missing some minor details.
You should watch some tutorials from phpacademy.org or thenewboston.org, they have best and most valuable tutorials ever made about PHP.
I think you may try to start from that course/playlist:
phpacademy.org: PHP Tutorials: Creating a Blog
If you don't understand everything, watch this:
thenewboston.org: Official Beginner PHP Tutorials Playlist!
If you have no problems with PHP itself you may try to use some simple framework with MVC support. That helps A LOT in variable handling between pages, makes work with database easier etc.
phpacademy.org: Introduction to CodeIgniter
phpacademy.org: Introduction to CodeIgniter - Basic Website
I had the same problem. You can easily do this by using the fopen function. Here is a link to a tutorial: http://www.tizag.com/phpT/filecreate.php
<?php
function wwwcopy($link,$file)
{
$fp = #fopen($link,"r");
while(!feof($fp))
{
$cont.= fread($fp,1024);
}
fclose($fp);
$fp2 = #fopen($file,"w");
fwrite($fp2,$cont);
fclose($fp2);
}
//Example on using this function
wwwcopy("http://www.domain.com/list.php?member=sample", "sample.html");
//Another example
wwwcopy("http://www.domain.com/list.php?member=sample2", "sample2.html");
?>

CMS permalink and templating system how to?

I want to understand how to use templating system and permalinks on php websites :D !..
let me describe my self more,
1.currently i have 20 files each have its own php logic (index.php,wizard.php,search.php etc)
ALL use same class's and includes.(install.php include all the required for all class's in my project abd u require_once(install.php) in all files)
i wana remodel my website into.
Analyze URL requested ---> IDENTIFY requested page ---> GET TEMPLATE for THIS PAGE -->MODIFY header(meta) and footer(javascript) ---> add logic ---> display page :D.
can some one put me on right learning track :D !. cuz i hv coded my website fully in oop and made all its content loads dynamically from MYSQL (simple small CMS) but i have no clue how to join template and php into index without repeating my self and create different file for each page in my web ! (each file do different jobs of course like file users.php does login and registration and userprofile etc)
Hope my english wassnt too too bad and u could understand my question :D !
My current approach so far:
Mysql table : page_tbl
columns: pagename,LogicFile,templateFile,MetaTag.
index.php?pagename
will check if not already cached or not listed for chacing it will :
Mysql:SELECT * WHERE pagename='$_GET['page']'
loginfiles = cars.php,search.php (will be exploded with , and included)
Template files = will be also exploded and modified according to MetaTag.
Metatag: Serialized assoc array with ['name']=['value'];
Then i start buffer output , replace template with new descriptions and keywords(auto generated)
include logic files
include footer.php (which include the scripts)
am i near to correct rout or still far away ? or am did i lose my track and over killing
If you are unsure whether you want to write your own, or use an existing one.
It is going to be a quite possibly very rewarding experience but a very time consuming one to write your own.
If you have a task at hand that you need to be solved, use an existing one.
That being said there are plenty of templating systems, smarty being the most long-lived one. You can find a short discussion on 5 popular ones at phpbuilder.
You also have entire Frameworks that you might want to consider. They are more than just a templating system, where database stuff, ajax helpers etc are included. Zend, CakePHP, and Codeigniter being popular ones. You can find comparisons of these at phpframeworks.
The best way is to take this route:
parse request data
determine controller which will be used
in controller select layout and template [layout "includes" template, it hold the contents that are common to all pages and template has the request-specific content]
in controller also fetch the data from database [MVP way]
assign data to array and pass it to the layout, it'll pass the data futher to the template
in layout and template use data from that array and construct view
pass everything to the browser
This is the way the frameworks work, and it isn't that bad. ;]

Create Pages Automatically

Is there any method in Php by which I can create a page automatically based on a predefined template. Like if we create a new post in blogger it automatically creates page for that post with the name of that post, like this one:
http://learntoflash.blogspot.com/2009/12/exit-button-in-flash.html
Here Exit Button In Flash is the name of my post I have written and an automatic page is created for it.
Or like here on this website if we ask a question it automatically creates a page for that question. I want to know can I achieve this in Php or anything close to this ?
...here on this website if we ask a question it automatically creates a page for that question.
It sounds like you may believe an actual file is created when you post a question. My bet would be that this page is generated via the question id in the URL.
The only files created would be cached output, which may or may not resemble actual HTML pages.
You should use URL rewriting. This Apache module lets you define rules to rewrite web addresses in your desired way.
The process to make your web application ready for this, is not a short story so you should read more about it.
This article is a good starting point:
http://articles.sitepoint.com/article/guide-url-rewriting
This is acheived by using mod_rewrite. A good place to look for inspiration is the .htaccess used in Wordpress.
to something like that you have to grasp the very fundamental in php or any programming language at all, i mean the core of php is to create dynamical generated pages based on user/browser input.
You might need to take a quick tutorial about php might I suggest http://www.tizag.com/phpT/
good step for step tutorial
Edit:
if you're wondering how the websites seems to have created a html page for every question, the answer would be they're not they are probably using mod_rewrite as mentioned before to rewrite to url to print a little more user friendly url, the actual url could be something like this https://stackoverflow.com/index.php?post=4499289 in reality

Categories