How do you make non-www. links contain www. in php? - php

Im storing imdb.com links for each movie thats listed in the DB, and check for duplicates before a new movie is inserted. The problem is, some links are http://imdb.com/whatever while others are http://www.imdb.com/whatever
What would be the best way to force www. into every link thats submitted? I realize I should be storing the url without http:// or http://www. which would alleviate this problem all together.... but its too late to make that decision now.

Why don't you just store IMDB's movie id rather than the entire URL? If you just store the ID then you can build the URL programmatically.
For Instance for this url http://www.imdb.com/title/tt1049413/ you could just store tt1049413. This is a better design in my opinion because if IMDB ever changes their URL format you can just change the part of your app that builds the url rather than changing every row with a bad url.

Use MySQL to fix the existing ones:
UPDATE table SET URL=REPLACE(URL,'http://imdb.com','http://www.imdb.com') WHERE URL LIKE 'http://imdb.com/%';
Then use PHP to fix inbound URLs beforehand:
$url = str_replace('http://imdb.com','http://www.imdb.com',$url);
But the best method is to store imdb.com's movie ID in your database instead:
http://www.imdb.com/title/tt0088846/
Store "tt0088846" instead, or even better, 88846 as your Primary Key, and use a constant:
$imdb_url = "http://www.imdb.com/title/tt{ID}/";
$url = str_replace("{ID}", $movie_id, $imdb_url);
That way it's much faster and easier to detect duplicates. Note that IMDB has different media types (actors, etc.) which use a different prefix (nm for actors, etc.) so be aware when designing your database.

As your storing the link, can't you check if it starts with http://imdb and replace that with http://www.imdb?

To answer your question, forcing non-www. links on submission would be a better option in my opinion, plus I'd update the database using razzed's solution.
$url = str_replace('http://www.', 'htp://', $url);
Still, I would store only the IMDB ID.

You could use regular expression to force the URL but not all host names start with www.

Related

Saving current page URL to MySQL database

I'm using a javascript step sequencer that records the current user-inputed drum pattern into the URL.
So for example before any user input the url looks like:
http://localhost:8888/member-index.php#/0000/0000/0000/0000/0000/0000/0000
and then if the user inputs a basic drum beat the URL might look like:
http://localhost:8888/member-index.php#/8020/0808/aaaa/0000/0000/0000/0000
So I want to be able to save the user-created patterns to my MySQL database so that user's can save and load beats they've previously created.
Could someone give me a quick example of what the PHP code would look like to save the pages current URL to a database?
EDIT:
People are saying to use $_GET - how would I use this with a URL like mine that is broken up into seven sections with "/" dividing them?
Short Answer
Use $_GET instead.
Long Answer
Retrieving the url with PHP isn't going to include what comes after the #, because that's only sent to the browser and not to the server. As #Kazar says in an answer to a similar question, you could use Javascript and document.location.hash to retrieve the information after the hash and then send it to the server via ajax.
But fortunately there's a much better built-in solution, which is $_GET (documentation here).
Instead of constructing your url thus:
member-index.php#/8020/0808/aaaa/0000/0000/0000/0000
Make it like this:
member-index.php?a=8020&b=0808&c=aaaa&d=0000&e=0000&f=0000&g=0000
Then you can retrieve this information easily in PHP:
$a = $_GET['a'];
$b = $_GET['b'];
...
And then pass it on to the database. (Even better, replace a, b, etc. with whatever the order actually means)
You could use htaccess and url rewriting to redirect all requests to a specific php in which you check the url. see:Rerouting all php requests through index.php
nevertheless I think using get/post or the request body is easier to send your data.

Converting parameter based URL's to pretty URL's

Currently I have url's in this format:
http://www.domain.com/members/username/
This is fine.
However each user may have several 'songs' associated with their account.
The url's for the individual song's look like this:
http://www.domain.com/members/username/song/?songid=2
With the number at the end obviously referring to the ID in the MySQL database.
Using jQuery/javascript, the ID is collected from the URL and the database is then queried and the relevent song/page is rendered.
I would like to change these URL's to the following format instead:
http://www.domain.com/members/username/song/songname/
But I have absolutely no idea how to go about it. I've been doing quite a bit of reading on the subject but haven't found anything quite relevant to my situation.
To further compound the challenge, song names are not always unique. For instance if we image the song name 'hello' it is quite possible that another song may exist in the database with the same name, albeit with a different song ID.
Given the limit information you are recieving in this question I am quite content with more generalised answers, describing the approach to take.
General info:
Apache/Nginx proxy
Backend: PHP
jQuery/Javascript front end
I don't know how do you store songs in the database but an idea:
use URL rewrite to rewrite members/username/song/songname/ to song.php?user=username&song=songname. There are plenty of tutorials here or perhaps try to use an URL rewrite-generator tool.
In song.php, get these GET values. Do a MySQL query where the songname and the username match. Output the result.
Note: it is OBLIGATORY to make that a user can store only one song with a given name. Also, the storing user's name MUST be stored. Else this is impossible.
Simple Apache rewrites, in the main httpd.conf file, or an htaccess file if you don't have access to the main config file should suffice

Best way to save info in hash

I have a webpage that the user inputs data into a textarea and then process and display it with some javascript. For example if the user types:
_Hello_ *World* it would do something like:
<underline>Hello</underline> <b>World</b>
Or something like that, the details aren't important. Now the user can "save" the page to make it something like site.com/page#_Hello_%20*World* and share that link with others.
My question is: Is this the best way to do this? Is there a limit on a url that I should be worried about? Should I do something like what jsfiddle does?
I would prefer not to as the site would work offline if the full text would be in the hash, and as the nature of the site is to be used offline, the user would have to first cache the jsfiddle-like hash before they could use it.
What's the best way to do this?
EDIT: Ok the example I gave is nothing similar to what I'm actually doing. I'm not cloning markdown or using underline or b tags, just wanted to illustrate what I wanted
Instead of trying to save stuff in the URL, you should use the same approach that is common in pastebins: you store the data , can provide use with url, containing an unique string to identify stored document. Something like http://foo.bar/g4jg64
From URL you get state or identifiers, not the data.
URLs are typically limited to 2KB total, but there is no officially designated limit. It is browser-dependent.
Other than that, make sure you properly URL encode what you're putting up there, and you're fine... although I certainly would not want to deal with obnoxiously long URLs. I might suggest you also avoid tags such as <underline> and <b>, as they have been deprecated for a very, very long time.
Use javascript function:
encodeURIComponent('_Hello_ *World*');

How can I create a user-generated page on my website?

For example, say if a user wanted to 'add a place' to my database, how could I create a page almost instantly with that place's name in the domain e.g www.mydomain.com/placename?
I understand it might be a complex solution so if it is too complex to explain can you please push me into the right direction of what I should be researching.
Create functionality to create "pretty urls" in php. Read more about that here: http://www.roscripts.com/Pretty_URLs_-_a_guide_to_URL_rewriting-168.html
Create parsing functionality for the urls, so that it recognizes "/placename" as the page "placename"
Create a database structure for pages with the page id, title, content and the url slug, etc.
Create functionality to fetch the right page from the database according to the matching url slug.
Create functionality to render the retrieved informaton
If I understood you right that's one approach to what you want to do.
I'm assuming you're using Apache. If so, create a rule using mod_rewrite that forwards requests for /place/placename to /place.php?name=placename. Then write the place.php script, which will pull the user page from the database and display it in the appropriate fashion.
That's one way to do it - there are others.
First of all try to understand mod rewrite.
You could "mask: a GET url into a much nicer format.
Start here : http://www.elated.com/articles/mod-rewrite-tutorial-for-absolute-beginners/
Then google on and get yourself familiar with all the possibilities.
After that make sure the GET variable is unique in your database, to be absolutely sure use a unique ID.
Example :
domain.com/PLACEID/PLACENAME/
mod_rewrite could then translate this to your php script into :
domain.com/place.php?VAR=PLACEID&VAR2=PLACENAME
Search the data from the user/place through the PLACEID .
Good luck

PHP Parsing URL

Ok, i'm trying to get data from a database to be pulled according to the url.
I have a database that is holding data for some announcements for individual customer websites. The websites are in individual directories on my website. (i.e., www.domain.com/website1/index.html, www.domain.com/website2/index.html, www.domain.com/website3/index.html, etc..) In the database i have a column that has each customers "filing name" (aka directory name - website1, website2, website3, etc..). I want to try and display only rows in the database where filingName = website1 for the domain "www.domain.com/website1/index.html". Hope this makes sense. I'm basically trying to figure out how to connect the dots between a single page and only pulling a specific customers records. Any thoughts??
Thanks!
Depending on how big your data set is, it might be "cheaper" to preprocess the URLs and store the individual components you need to match on. e.g. create extra fields for host/dir/querystring and store those individually, instead of a monolithic absolute URL. This would be safer than trying to do substring matches, especially if the substring you're matching could be part of multiple different urls ('abc' being part of 'abc.com' and 'abctv.com').
Use a like statement in your query:
SELECT * FROM `sites` WHERE `url` LIKE "%/website1/%";
Your best bet is going to be to store these identifiers on their own in the database (ex 1= website1, 2 = website 2) and name the directories accordingly. Then in your .htaccess file, manipulate the URL to look like your $_GET variable is the directory name, replace it with the matching name in your database. Do this with RewriteRule rule. This is the most absolute way to achieve this while keeping the same URL structure.
edit: whoops, didn't realize how old this thread was.

Categories