I have a website where each person has his personal profile. I would like to have static URL like mywebsite/user1, mywebsite/user2, but actually I would remain in the same page and change the content dynamically. A reason is that when I open the site I ask to a database some data, and I don't want to ask it each time I change page.
I don't like url like mywebsite?user=1
Is there a solution?
Thank you
[EDIT better explenation]
I have a dynamic page that shows the user profile of my website. So the URL is something like http://mywebsite.me?user=2
but i would like to have a static link, like
http://mywebsite.me/user2name
Why I want this? Because it's easy to remember and write, and because i can change dynamically the content of the page, without asking each time data to my database (i need some shared info in all the pages. info are the same for all the pages)
Yes there are solutions to your problem!
The first solution is server dependend. I am a little unsure how this works on an IIS server but it's quiet simple in Apache. Apache can take directives from a file called .htaccess. The .htaccess file needs to be in the same folder as your active script to work. It also needs the directive AllowOverride All and the module mod_rewrite loaded in the main server configuration. If you have all this set up you need to edit your .htaccess file to contain the following
RewriteEngine on
RewriteRule ^mywebsite/([^/\.]+)/?$ index.php?user=$1 [L]
This will allow you to access mywebsite/index.php?user=12 with mywebsite/12.
A beginner guide to mod_rewrite.
You could also fake this with only PHP. It will not be as pretty as the previous example but it is doable. Also, take into concideration that you are working with user input so the data is to be concidered tainted. The user needs to access the script via mywebsite/index.php/user/12.
<?php
$request = $_SERVER['REQUEST_URI'];
$request = explode($request, '/'); // $request[0] will contain the name of the .php file
$user[$request[1]] = $request[2];
/* Do stuff with $user['user'] */
?>
These are the quickest way I know to acheive what you want.
First off, please familiarise yourself with the solution I have presented here: http://codeumbra.eu/how-to-make-a-blazing-fast-ajax-call-to-a-zend-framework-application
This does exactly what you propose: eliminates all the unnecessary database queries and executes only the one that's currently needed (in your case: fetch user data). If your application doesn't use Zend Framework, the principle remains the same regardless - you'll just have to open the database connection the way that is required by your application. Or just use PDO or whatever you're comfortable with.
Essentially, the method assumes you make an AJAX call to the site to fetch the data you want. It's easy in jQuery (example provided in the article mentioned above). You can replace the previous user's data with the requested one's using JavaScript as well on success (I hope you're familiar with AJAX; if not, please leave a comment and I will explain in more detail).
[EDIT]
Since you've explained in your edit that what you mean is URI rewriting, I can suggest implemensting a simple URI router. The basics behind how it works are described here: http://mingos.eu/2012/09/the-basics-of-uri-routing. You can make your router as complex or as simple as needed by your application.
The URL does not dictate whether or not you make a database call. Those are two separate issues. You typically set up your server so example.com/username is rewritten internally to example.com/user.php?id=username. You're still running PHP, the URL is just masking it. That's called pretty URLs, realized by URL rewriting.
If you want to avoid calling the database, cache your data. E.g. in the above user.php script, you generate a complete HTML page, then write it into a cache folder somewhere, then next time instead of generating the page again the script just outputs the contents of the already created page. Or you just cache the database data somewhere, but still generate the HTML anew every time.
You could write an actual HTML file to /username, so the web server will serve it directly without even bothering PHP. That's not typically what you want though, since it's hard to update/expire those files and you also typically want some dynamic content on there.
Select all from your database.
Then create file containing the scripts contents(index.php?user='s) for each one. set the file name to user_id/user_name you got from the SELECT statement.
This will create a page for each user in the present folder.
To avoid having to recreate 'static' pages, you could set a new column named say 'indexedyet' and change it to 1 on creating a file. You select only files which have this as 0. You could perform this via cronjob once a day or so.
This leaves you vulenderable to user data changes though, as they won't autmatically update. a tactic to use here is to update the static page on any editing.
Another, probably better (sorry not had enough coffee yet-) ideal would be to create a folder on a users registration. Make the index.php page tailored to them on registration and then anything like www.mysite.com/myuser will show their 'tailored version'. Again update the page on user updates.
I would be happy to provide examples depending on your approach.
Related
I just recently started webdevelopping/programming. And I'm starting to like it now I'm beginning to get the grasps of php programming. Altough I came across a problem I can't fix/don't understand how it possibly can be solved.
I've created a "scroll back to top" button and I'd like switch it on or off on the admin page. That's also the problem. I don't know how to properly do that. Do I have to create a Sql table to store a value and call the value in the button script or is there another way?
Right now the button and jquery function is scripted in a seperate file which I include on the pages where I need the button. I tries to create a form in the admin page which submits a value (on or off) to a config file and the config file is included in the button script file. Right now I feel this isn' the right way to do acheive what i'd like to.
Is there anyone who could help me, and other starting developers facing the same problem, by explaining how this can be acheived or pointing me to some resources/tutorials on how to do such things in PHP?
My gratitude will be of extraterestial magnitude!
Well, it depends if you already have a database. I wouldn't create a database, or even a table just for that. I would consider simply writing an xml file to the file system for something so simple. If you have a multi-user site and need it customer per user, then that would justify a db table since you'll likely be adding more to it later.
If you aren't worried about persisting it between visits to the site, you could just use $_Session: http://php.net/manual/en/reserved.variables.session.php
Maybe even in a cookie if you don't want to do db work and need cross session persistance: http://php.net/manual/en/reserved.variables.cookies.php
Check this for the scrolling question, this isn't php: https://developer.mozilla.org/en-US/docs/DOM/window.scroll
Check this post, it may help: Creating/Writing an XML file in PHP?
My problem is not so easy to describe ... for me :-) so please be lenient towards me.
I have several ways to view a list. which means, there are some possibilities how to come to and create the view which displays my list. this wokrs well with parallel opend browser tabs and is desired though.
if I click on an item of my list I come to a detail-view of that item.
at this view I want to know from which type of list the link was "called". the first problem is, that the referrer will allways be the same and the second: I should not append a get variable to the url. (and it should not be a submitted form too)
if I store it to the session, I will overwrite my session param when working in a parallel tab as well.
what is the best way to still achive my goal, of knowing which mode the previous list was in.
You need to use something to differentiate one page from another, otherwise your server won't know what you're asking for.
You can POST your request: this will hide the URL parameters, but will hinder your back button functionality.
You can GET your request: this will make your URLs more "ugly" but you should be able to work around that by passing short, concise identifiers like www.example.com/listDetail?id=12
If you can set up mod_rewrite, then you can GET requests to a url like www.example.com/listDetails/12, and apache will rewrite the request behind the scenes to look more like www.example.com/listDetails?id=12 but the user will never see it -- they will just see the original, clean/friendly version.
You said you don't have access to the server configuration -- I assume this is because you are on a shared server? Most shared servers already have mod_rewrite installed. And while the apache vhost is typically the most appropriate place to put rewrite rules, they can also be put in a .htaccess file within any directory you want to control. (Sometimes the server configuration disables this, but usually on a shared host, it is enabled) Look into creating .htaccess files and how to use mod_rewrite
I'm currently trying to create a CMS using PHP, purely in the interest of education. I want the administrators to be able to create content, which will be parsed and saved on the server storage in pure HTML form to avoid the overhead that executing PHP script would incur. Unfortunately, I could only think of a few ways of doing so:
Setting write permission on every directory where the CMS should want to write a file. This sounds like quite a bad idea.
Setting write permissions on a single cached directory. A PHP script could then include or fopen/fread/echo the content from a file in the cached directory at request-time. This could perhaps be carried out in a Mediawiki-esque fashion: something like index.php?page=xyz could read and echo content from cached/xyz.html at runtime. However, I'll need to ensure the sanity of $_GET['page'] to prevent nasty variations like index.php?page=http://www.bad-site.org/malicious-script.js.
I'm personally not too thrilled by the second idea, but the first one sounds very insecure. Could someone please suggest a good way of getting this done?
EDIT: I'm not in the favour of fetching data from the database. The only time I would want to fetch data from the database would be when the content is cached. Secondly, I do not have access to memcached or any PHP accelerator.
Since you're building a CMS, you'll have to accept that if the user wants to do evil things to visitors, they very likely can. That's true regardless of where you store your content.
If the public site is all static content, there's nothing wrong with letting the CMS write the files directly. However, you'll want to configure the web server to not execute anything in any directory writable by the CMS.
Even though you don't want to hit the database every time, you can set up a cache to minimize database reads. Zend_Cache works very nicely for this, and can be used quite effectively as a stand-alone component.
You should put your pages in a database and retrieve them using parameterized SQL queries.
I'd go with the second option but modify it so the files are retrieved using mod_rewrite rather than a custom php function.
We have an existing PHP page (from an earlier project) which could be described as cryptic and ancient. It basically displays a form, catches the input and runs an external application to process the input and then pipes the output to the user.
I would really like not to modify this file any more than is required. Would there be an easy way to just make this file magically work by copying it to some location in the CakePHP's directory and have it receive $POST etc. as usual?
Given that this is a simple form you described, I would simply move that form to a view, handle the post in a controller (possibly converting cake's POST data to your form's "old format" and "forwarding" it to the old page code). You could then redirect to a result page or just output it in the same view. It could be a simple copy-paste job, but only you know if that's true. But if it is really that simple, a conversion is due ;)
I'm afraid there is no way do just "plug it in" and have it working, or at least I'm not aware of any way to do that.
Unless you're willing to leave that form in your /app/webroot and keep it separate from the rest of your app? Anything placed there should be left alone by cake, so I guess that would work..?
I'm starting a experimental project and I was looking for advice to choose the best option I have.
The project will be on the 100 - 1,000 user count. It collects its main data using javascript + json data from the user's flickr page and then uses that to display specific flickr photos. The variables that need to be stored include user specific URL slug, and maybe 4 more short string variables. That all will need to be looked up on each page request. These variables will not change by time unless the user visits update page, and so these variables are static 99% of the time.
Each user's page will be located at /user-slug
I have thought out three options though I do not want to limit myself to these three (please offer your own), not being an experienced coder at higher access counts, I was looking for the fastest, most static & cacheable, least resource consuming way of achieving this, and I'm sure you guys are far more clever than I am at achieving this.
for the N amount users:
completely static approach: N static html pages are created, each user page is updated whenever asked, htaccess mod-rewrite is used to make each html file resemble a directory access. Updating; Php is used to rewrite the static pages when user asks them to be updated, or a full N user rewrite is performed when template needs updating. Most of the in-development code resides in a javascript file so the template itself will probably not be edited as frequently. Each time a user page is called, a static html file is displayed, javascript collects data from flickr server and displays it.
half static approach: Php + mod rewrite is used to simulate the different N user pages, user slug and only user slug is stored in MySQL database, then user specific variables are loaded via individual unique static texfiles named after the user slug (user-slug.txt) via javascript by the browser client (this data is not sensitive). Each time a page is called, 1 MySQL call is made and 1 extra txt file is loaded in the header via javascript. Javascript collects data from flickr and displays it.
full dynamic approach: Php + mod rewrite is used to simulate the different N pages (as the above method). All user specific variables are stored in MySQL. Each time a page is called, about 4 MySQL calls are made, Php creates the template page using those variables. Javascript collects data from flickr and displays it. In this method, which I believe is the more common approach to multi-user websites, I am also looking for ways to make these php/MySQL calls cacheable on the server itself. I'm on shared hosting btw, I don't have any low level access to the configuration itself.
Thank you so much for your input
Very, very appreciated!
I'd start with the full dynamic approach.
Then based on profiling and performance move those parts to caching that cost the most resources.
As they say 'premature optimization is the root of all evil'. Don't try to think what will take the most resources, but measure it by profiling time and memory usage.
I'd go with full dynamic as well. Though try your best to make whatever javascript/css you have static so it can be linked from an external file and not generated.