Create redirect URL - php

Here is the real case, in the NewsNow.co.uk, there are many link of uptodate news from thousands of website. And the example for one of the news url:
http://newsnow.co.uk/A/471722742?-19721
all the news url are formated like that, but when we click it, we will be brought to the real url, for ex:
http://www.abcactionnewsx.com/dpp/news/state/bla-bla
anyone know how to achieve this efficiently ?

Store a table of 'internal' paths (the 'newsnow' urls) and the 'destination' urls in a database of some sort; sqlite3 would be a fine choice for smaller applcations.
You could hash the 'internal' paths if lookup time for specific strings was too slow in the database you chose.
When a request comes in, look it up in the database and send back a 302 response with the 'target' URL as the new location for the resource.

This is done using a rewrite engine that is built into common webservers like Apache or Nginx. These engines allow you to write rules that transform a url like the first one into something that would be better understood by your php pages. For example, you could create rules that would turn your first link above into:
http://newsnow.co.uk/index.php?catagory=A&id=471722742&referrer=-19721
This is transparent to the user and they will only ever see the link they first typed in. You can then use the variables being passed in to perform whatever actions you desire. In this case you might want to use the variables to perform some kind of database lookup to retrieve the actual destination that you are interested in. Then it's just a question of performing a php redirect to the link in question.
Check out the following link for a very quick intro to Apache's rewriting capabilities (called mod_rewrite): http://www.besthostratings.com/articles/mod-rewrite-intro.html

Related

Web translating - single or separate files for different languages?

SHORT DESCRIPTION
It will be Lithuanian - English website (only 2 languages).
This will be jobs listing website, so content shouldn't be translated. If employer provided job in English so this should be in English even if Lithuanian language is selected. There should be translated only field names, buttons & error messages.
QUESTION IN SHORT
What is better way to have single file index.php for both languages or create separate files for LT and EN languages?
1st WAY
So I can make It in few ways, for example:
www.mysite.com/jobs -- default Lithuanian language
www.mysite.com/en/jobs -- English language
So in this case I need to have 2 duplicate websites (just translated) Lithuanian stored in root/ folder and English stored in root/en/ folder? So If I change something in Lithuanian website I need to make exact changes in English website?
It not looks like good practice to write code twice.
2nd WAY
Create variable something like $lang = "en";, store all field names in database in way like this:
Id FieldName_lt FieldName_en
1 Vardas First Name
2 Pavardė Last Name
3 El. paštas E-mail
And select from database via PHP like SELECT FieldName_ . $lang FROM table..., but It could be SQL Injected If I'll use variable in SQL query.
3rd WAY
Maybe It's better way to store field names in arrays (there will be maybe 150+ fields)?
If I'll go for 2nd or 3rd way, should I save language choice in cookies? So in this way website url always will be like below?
www.mysite.com/jobs.php?lang=en
www.mysite.com/jobs.php?lang=lt
Maybe there is another way to make It, without showing language choice in address bar at all? Or It's bad practice to hide language choice form address bar?
ADDITIONAL
In HTML form I'm using data validation in following:
<input type="text" id="first-name" placeholder="" required
data-validation="length alphanumeric"
data-validation-length="3-12"
data-validation-error-msg="User name has to be an alphanumeric value (3-12 chars)"/>
So how about error message translation?
The same as database approach you can use static file for each language and translation.
en.php
return [
"somekey" => "English Translation"
];
lt.php
return [
"somekey" => "Lithunian Translation"
];
You can then mod rewrite to get language from url if you want some directory structure, or simple query parameter or cookies (as specified by others). If you are using any any RESTfull service it is also possible to set it in HTTP header. Many frameworks also there to help you parse data from url out of the box.
$langCode is language code fetched from Query PAram, url path, header or cookie
you can also use http://php.net/file_exists to check if translation file is available or not before you use require_once to pull the translation resource.
Once you get the language code you can just use
$stringResource = require_once "lang/{$langCode}.php";
Then you can fetch all the resource by its key from $stringResource.
<?php $stringResource = require_once "lang/{$langCode}.php"; ;?>
<input type="text" id="first-name" placeholder="" required
data-validation="length alphanumeric"
data-validation-length="3-12"
data-validation-error-msg="<?php echo $stringResource['somekey'] ;?>"/>
You can just edit the translation in editor. wont need to connect to database and as it is just assoc array. it would be way faster.
Query:
www.mysite.com/jobs.php?lang=en
as already mentioned it will be ignored in term of SEO. Query parameters are ignored by crawlers.
URL Path
www.mysite.com/en/jobs.php
here you need to do mod rewrite http://httpd.apache.org/docs/2.0/misc/rewriteguide.html which is basically just catch url and fetch out the en part and rewrite to something like www.mysite.com/jobs.php?lang=en
Both data can be get from $_GET['lang']. but url path will have benefit on SEO.
Cookies
Will not be shown in address bar. but that also means if the link is shared to another user they will not see the language of origin they will see default language.
https://support.google.com/webmasters/answer/182192?hl=en#1 as per google doc. i believe it would be nice to do it using url path.
I think the approach with the Database is fine. Whenever you want to change the translation for something you will just have to edit the table entry. You can write a class which doeas the translation for you, so you only have to pass the language-id and the language.
How to save the language/ how to display it: It depends on how it is meant to be used. If it is likely people often share a link to your site, you could inclue the language in the url, e.g. as a GET paramater. If it should "just" stay the same for the user who visited the site, cookies are a nice approach.
Using a cookie to store the language preference is an option but might cause some issues for SEO and this will be relevant for a job site.
Without using cookies you can either put the language in the directory path as you suggested. You don't need to have 2 separate websites then, you can use url rewriting to change a part of the path into a query parameter (for apache use mod_rewrite).
Using just a query param, just as your second suggestion will work too but looks less nice.
Make sure you offer your users a option to switch language, for example using flag icons or just text links.

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

Shortening URL containing query strings

I'm using Codeigniter(PHP Framework) and I have URLs like http://www.mysite.com/age/21/gender/female/name/jamie/city/boston/userid/1234.
Is it possible to make the URL more SEO/user friendly? Like http://www.mysite.com/1234-Jamie-Boston and somehow still be able to pass the values found in the original URL strings like age =>21 and gender => female?
I think you could - use mod_rewrite to rewrite your new url from http://www.mysite.com/1234-Jamie-Boston to http://www.mysite.com/user/1234. In your user controller load from the db the one with id 1234 and set all his other properties
Or without the mod_rewrite - you could modify your application/config/routes.php file with an entry like:
$route['(\d+)-.*'] = "users/display_user/$1";
This way your SEO friendly urls will point to class Users, method display_users and you will get the id as parameter
The way that PHP works is that if you send 15 parameters to a function which only requires two, then PHP can more or less "swallow" the other arguments (they're still accessible, but they are less than optional). In your case, I would create a Users controller, perhaps with a display function which only takes one parameter, the user ID. That way, you can have /users/display/1234/whatever/you/would/like/zodiac_sign/stop_sign/favorite_letter/favorite%20punctuation/... Need I go on?
You can get around the need for a controller/method as part of your URL by using the $routes config file... In this case, I don't think user/display would really hurt SEO. I would not use mod_rewrite to do that simply because the architecture is already there in CodeIgniter.

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

Categories