Passing many argument to controller function in Codeigniter - php

I have a controller named as Tag which is related to handling posts related to certain tags as wish by viewer
I want to make URL look like this
http://www.mydomain/tag/xxx/xxx
where the viewer can filter the post via using as many tags as they like
e.g
http://www.mydomain/tag/XXX/XXX/.../..../...
I have tried this via using regular expression in routes.php
$route['tag\/[a-zA-Z0-9\/]']='tag/index';
and filtering the URL in index method of Tag controller but it didn't work for more than 1 tag. I want the user can pass as many tags as they wish. The methods said in this question also didn't work.Is there any way to do this for index method?

If you are doing this way, Hacker might be hack your application and harm valuable data.
It will make your site vulnerable.
so,I think you should go for another way...
/controller/method/red+tall+fat

Related

How to structure hyperlinks when using Clean URL or Pretty URL

I've tried to search for the best way to handle links on a dynamic website when using "Clean" or "Pretty" URLs, but have not been able to find anything.
I've found a LOT of information on how to use mod_rewrite and try_files, which I've implemented successfully, now my php front controller parses all the parameters on a URL, and it links to the page correctly. No problem here.
The issue I'm having is how to best build all the links on my pages. Currently my links are all in the format eg. "www.site.com?do=post&id=23" which works fine. However I'd like them to display as "www.site.com/post/23" which already also works fine, because of try_files, and how my front controller parses all the parameters from the URL.
However do I now go through all my code and change all the dynamically built links to build in the "www.site.com/post/23" format? It seems like a lot of work, and to be honest I'd like to leave it an "option" to either use Clean or Dirty URLs, similar to how Wordpress allows it as an option in their Admin panel.
Do sites like that keep links in the format "www.site.com?do=post&id=23" and use a rewrite function on all the links when the page is created? So the links show up as Clean when the user sees it?
I'm confused as the best way to handle this, and hope I explained what I'm looking for. I just want to know how best to handle the dynamic links and have it optional to display as clean or dirty url format, for lack of a better word.
Thanks for any help.
That's a very conceitual question...
You could see how Laravel framework router works.
You do not need to use it, but you can get ideas there.
The router class is responsible of know how to create a url to some resource, page, action, whatever you want.
So in your view you just call a method that return the url.

How to create dynamic permalink URLs from $_POST data

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.

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");
?>

How to open a post in PHP when clicking it, a good approach

So I have a list of posts and when clicking on it, it should direct me to that particular post. Basically the way I code this is adding the id of the post to the end of the link, like posts/1 which will take me to the post with the id of 1. This is a basic design, because I see on many sites they do not have such urls...do they post the data to the new page? Or what is a better design to do this?
This is exactly how StackOverflow (among others) does this and it is good practice indeed. This is the URL of your post:
http://stackoverflow.com/questions/9257811/how-to-open-a-post-in-php-when-clicking-it-a-good-approach
The "/questions/9257811" segment is the id of the post. The other part is optional and is only used for Search Engine Optimization. Try to load http://stackoverflow.com/questions/9257811/WRONG-DESCRIPTION and see for yourself.
Edit: I couldn't use a wrong description in the above link, StackOverflow was correcting it automatically. It's easy to get the title once you get the id, which is how they autocorrect it. This avoids links like "/posts/34234/offensive-words-here" for example.
What you describe is the best way to send and receive an id in the URL. Most webpages do it this way. You have a link (example: Post #1.).
When applying url-rewrite it simply "prettify" the url by making it look this way http://www.yoursite.com/post/1, but in fact, the url is actully still the same. You can access it with $_GET['id'] was usual.
I beg to differ. Though most sites uses it (and i admit, numericals are more convenient for refer), a friendly approach is, using the title of the post. But you need to do a lot of extra steps to make it error free (such as, no post can have identical titles, using a dash or underscore between the words for flexibility plus filtering the url should be more strict and becomes complicated in this case). If you want to avoid all these hussle, use ids as they are easier to implement.

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