# how youtube channel url works



## Mr.LLB (May 27, 2008)

hey guys,

i want people to be able to create channels on my website and want to give them similar channel url to how youtube does it.

youtube.com/XYZ

how is such a url processed? Can such a url be processed in php?

I want people to be able to go to MYSITE.com/XYZ and this directly to go to channel XYZ

Thanks


----------



## ehymel (Aug 12, 2007)

You can process with php, but probably better to process through apache using mod_rewrite. See here.


----------



## Mr.LLB (May 27, 2008)

thx ehymel,

it seems like you're the only one on this site helping!

how would I do it with php?


----------



## ehymel (Aug 12, 2007)

lol, I think you just ask questions that I know a little something about!

Probably several ways to handle this, and it depends a lot on how you set things up. The down-side to processing with php is that the URL being requested has to actually exist, whereas processing with apache means you can transparently redirect an arbitrary request to a definite script. This is a really huge difference, especially if you plan on having more than a few channels.

To handle with php, then *every* channel would require an actual file to exist to handle *each* channel, even if each of those files does nothing more than reference some common script somewhere that shows the appropriate channel content. What a pain!

To handle with apache, then requests to http://mysite.com/xyz shows you channel xyz, mysite.com/abc shows you channel abc, etc. Now multiply by a dozen or a hundred or thousands of sites, and the power is clear in using apache.

If you *really* want to use php, then you could get at the requested channel by parsing $_SERVER['PHP_SELF'].

Did I mention that apache is really the way to go on this? ;-)


----------



## Mr.LLB (May 27, 2008)

lol yes. but the reason i am asking about php is because that's the only server side language i know.

now...can't i do this.....have the link there

get php to get the url, break it down, get the channel name entered, search for it, and redirect to that page? and if the channel page does not exists, redirect to the index page


----------



## ehymel (Aug 12, 2007)

No, that won't work. If the page does not exist, then apache will complain before php ever sees a request. You can configure apache to use a custom 404 error page (does not exist error) that could process the request.

The apache redirect stuff is really not too difficult. If you can do the php and javascript I've seen from your work, then the apache redirect will be easy.


----------



## namenotfound (Apr 30, 2005)

Mr.LLB said:


> lol yes. but the reason i am asking about php is because that's the only server side language i know.


Coding in Apache is pretty simple, create a text file and name it *.htaccess* and that file is where you write all your code.

Here's an example of "myname.php" redirecting to "myname"

RewriteRule ^myname(/)?$ http://example.com/myname.php

The (/) means that "example.com/myname" and "example.com/myname/" will both redirect to "example.com/myname.php


----------



## Mr.LLB (May 27, 2008)

thanks to both of you, i have been reading tutorials on this.

@mynamenotfound

does this mean that i have to manually write the redirect code for EACH redirect I want?

there's no automatic way?

thx


----------



## ehymel (Aug 12, 2007)

No need to write the code for each one, just use wildcards.

For example:
RewriteRule ^/(.*)$ /channel.php?channel=$1

Then in a script called channel.php. process $_GET['channel'] to grab the channel name. I haven't tested this, so be wary that this example might redirect *everything*. I'm just illustrating the strength of wildcards in this scenario.


----------



## Mr.LLB (May 27, 2008)

ahh...

got it. thanks man.

can you use this sort of trick to hide your javascript file too?

as a security addon to my site, can i use this type of redirecting to hide the location of my javascript file?


----------



## ehymel (Aug 12, 2007)

Mr.LLB said:


> as a security addon to my site, can i use this type of redirecting to hide the location of my javascript file?


Hmmm, I'm not sure how that would work. Your client's browser has to be able to find the javascript file, so a determined user could find it.

You could use a trick I've used for some images. It's not perfect, but the basic idea is that your "hidden" file is a php script that process the $_SERVER['HTTP_REFERER'] variable to make sure your script is being requested by a specific URI. If not, no script gets sent. To a client browser processing a simple <script src="blah"> tag, it sees what it expects. If someone just tries to download the script, they see an error. I've not tried this for javascript files, so I'm not sure how it would do with something like firebug, but it might slow down some less sophisticated folks.


----------



## Mr.LLB (May 27, 2008)

so you are saying have php echo out script if the referrer is the correct one otherwise a blank page or something?

thats pretty neat too!

with my previous question, i was concerned with the security of my backend php files when using ajax.
the url we talked about helps hide file extension. which is great. but what about ajax now? right!


----------



## ehymel (Aug 12, 2007)

Mr.LLB said:


> so you are saying have php echo out script if the referrer is the correct one otherwise a blank page or something?


Essentially yes, although it doesn't have to actually be an echo. Just tack something like this to the top of your javascript file and rename the file to end in .php just so the server knows to process the script as php.


```
<?php
$referer = $_SERVER['HTTP_REFERER'];
$rhost = parse_url( $referer, PHP_URL_HOST );
if ( $rhost != 'mysite.com' )
   exit;
?>
-- regular javascript page goes here --
```



Mr.LLB said:


> but what about ajax now? right!


Same thing here, just check the referer and refuse to do anything if not requested by your known source.


----------



## namenotfound (Apr 30, 2005)

However if people disable referrals in their browser, that won't work. They'll most likely see nothing that is blocked if referrals don't match up.


----------



## ehymel (Aug 12, 2007)

namenotfound said:


> However if people disable referrals in their browser, that won't work. They'll most likely see nothing that is blocked if referrals don't match up.


Hmm, I don't know about this, but I'm curious. How & why would people disable referrals in their browser? Thanks in advance!


----------



## Mr.LLB (May 27, 2008)

lol..I dont even know how you would disable referrals!


----------



## namenotfound (Apr 30, 2005)

ehymel said:


> Hmm, I don't know about this, but I'm curious. How & why would people disable referrals in their browser? Thanks in advance!


I typically keep referrals disabled in my browser, because I don't like websites keeping a log of where I've been or how I got to their site. (if it says I got to X site via clicking a link on Y site, then X site knows I've been to Y)

I know a lot of people that disable referrals for the same reason. About 50% of all my friends disable referrals (the other 50% either doesn't care, doesn't know how, or prefer to keep them enabled)


----------



## ehymel (Aug 12, 2007)

namenotfound said:


> I typically keep referrals disabled in my browser, because I don't like websites keeping a log of where I've been or how I got to their site. (if it says I got to X site via clicking a link on Y site, then X site knows I've been to Y)
> 
> I know a lot of people that disable referrals for the same reason. About 50% of all my friends disable referrals (the other 50% either doesn't care, doesn't know how, or prefer to keep them enabled)


Interesting... but I suspect that overall very few people do this. If you are really wanting that sort of anonymity, why not just use an anonymous proxy?

LLB - Disabling of referrals would be easy enough to check for, and if your application requires then just inform the client.


----------



## Mr.LLB (May 27, 2008)

I am guessing that a very limited number of people would disable referrals. Merely those who know what it is and how to disable it.

The project I am working on is a real-estate related project. So I am sure there will be very few people visiting who have disabled referrals.

I guess I'll just use your method ehymel and inform the user that referrals must be enabled.

When you click to view the source of website, is the code loaded again or just viewed off of the opened page.
Cuz, if its viewed off the loaded page, then the person viewing source will still be able to read the javascript. If not, then I guess the php will echo out nothing.


----------



## ehymel (Aug 12, 2007)

Mr.LLB said:


> When you click to view the source of website, is the code loaded again or just viewed off of the opened page.
> Cuz, if its viewed off the loaded page, then the person viewing source will still be able to read the javascript. If not, then I guess the php will echo out nothing.


No, if they right-click on the page and view source, the source is pulled from the browser cache (i.e., not from the server). So the javascript will not be there if it gets loaded from an external file using a <script src="mysite.com/js.php"> type of line. If you have it in the body of the page itself, then yes it will be there. The method I described is meant for an external file. Sorry for the confusion!


----------



## Mr.LLB (May 27, 2008)

oh np!

yea i was referring to securing the backend php file from ajax when the file source is viewed.

is there such a way to coverup the location? or even encrypt the entire javascript file?


----------



## ehymel (Aug 12, 2007)

Mr.LLB said:


> is there such a way to coverup the location? or even encrypt the entire javascript file?


Not sure, or at least beyond my knowledge.

Mabye you could start a new thread and see if you get any bites?


----------



## Mr.LLB (May 27, 2008)

k.

thx for all ur help!


----------



## juliensoucy (Feb 20, 2011)

You can do this with php and Mysql,

Example:

Have a file name channel.php

Then when a user will click on a link to the specified chanel url will look like this
www.mysite.com/channel.php?id=1992

This method use the $_GET['id]

Then in your channel.php
Send a request to mysql database (ex: select (*) from table where id=$_GET['id']), gather all the information you need and just print it.

If you need code just reply i will post some


----------



## ehymel (Aug 12, 2007)

juliensoucy said:


> Send a request to mysql database (ex: select (*) from table where id=$_GET['id'])


This is horrendous code, *do not use anything like this*. Passing raw $_GET or $_POST data straight to a database is a huge security risk.


----------

