How to access a website's url programmatically from within the page - php

For work, I am working on a site that has a feature that lets users make custom instances of the site depending on who they want to show it to. So if they want to show their potential employer a little bit about themselves, they can send them a custom url, that has a uid on the end of it that tells the database what to show and what not to when the site is loaded up.
Now, I need to be able to take the value that is on the end of the url, this unique uid that corresponds with their preferences in the db, accessable on run time. Meaning that when someone types in this custom url, I need to be able to show the name of the person whose portfolio is being viewed BEFORE password authentication takes place...
If I could get the URL, I suppose I could parse through it to find the necessary UID...but how do I get the url? Maybe I am just missing something here, but if anyone has any thoughts or ideas, it would be appreciated!! Thanks
Sorry, to further clarify, I am using PHP to handle calls to the database...

Did you know dynamic web site technologies do just that?
There are lots of them and it is fairly easy to get the parameters from the URL.
Examples :
PHP:
http://www.example.com/show.php?uid=123
$_GET['uid'];
JSP:
http://www.example.com/show.jsp?uid=123
request.getParameter("uid");
There are so many of them it's pointless to enumerate them here

If you are using a server-side language, you can get any part of the url easily, for example, in php, it can be gotten through $_GET array and other arrays. If you want it with javascript, you can do like:
JS Example:
var url_array = document.location.href.split('/');
echo url_array[0]; // part one
echo url_array[1]; // part two
echo url_array[2]; // part three
You can even get the host part with:
alert(document.location.host);
PHP Example:
www.mysite.com?id=100
echo $_GET['id'];

This would depend on what language you are using, but typically that uid at the end of the URL would be passed as a request parameter, like this:
www.mysite.com/info?uid=dkafdojoapjdopakd
You would then access it in your code using the request parameters. So, in PHP, it would be:
$uid = $_GET['uid'];
// get the appropriate data for this uid, write out the page contents, etc.

First store each user's preferences as a piece of css within html style tags
+-----------+---------------+
| UID | style |
+-----------+---------------+
| 0z65dbr | <style>h1{ |
| | font-family: |
| | arial, |
| | sans-serif |
| | }</style> |
+-----------+---------------+
Then setting the get variable 'uid' in the url eg. www.example.com/page?uid=0z65dbr you can extract the value form the uid variable in php with $_GET['uid'] and pass this value to your sql query:
$sql = "SELECT style FROM styles WHERE UID = '".mysql_real_escape_string($_GET['uid'])."'";
Then in the put the result of executing the query into the head section of the html.

Related

How to store different type of data depending on selected option

I'm currently creating a contest system in PHP. For the database, I use MySQL.
When a users wants to make an submission, he can chose between 2 types of entry:
Link to a program
The program itself.
On my website, users can also create programs using a full coding environment I provide on the site. These programs then get stored to my database in the table scripts, which has the colums
user (representing the user ID)
script_id (representing the script ID)
name (representing the name of the program)
script (LongText representing the code of the program itself)
Now, if a user was up to make an submission, he can decide whether his submissionshould be a link to a program or a program he previously created on my website.
If he goes for the full program, he'll have to select one from a dropdown.
My question is all about the server side.
When the user than clicks "Submit", a JSON formatted string containing the type of the submission (0 or 1) and the payload (a long string containing either the link or the script_id).
Now on the server side, I got these information and I want to store the user's submission.
Now here's the actual question:
How can I store this information so that I can easily access the submission's type and its value (either a link or a full source code)?
I do not want to save the whole sourcecode of the script once again, because the user already did that, it's stored in the table scripts.
I thought about something like:
*---------------------------------*
| submission_type | value |
*---------------------------------*
| 0 | google.com |
*---------------------------------*
| 0 | stackoverf |
*---------------------------------*
| 1 | 3 |
*---------------------------------*
| 1 | 10 |
*---------------------------------*
But that would be kind of messed up, because I do not want to have either a string or an int in the value column.
What do you think is the best way to store this data of unknown type?
In the db you only need a table scripts and a table submission.
In the PHP page when user select "Link to a program" you call the add submission function with: USER_ID, SCRIPT_ID
But if user select "The program itself" first you call a function to create the script using the LongText representing the code of the program itself. After that you now have a script_id so call the submission function as above.

Use mysql to create a modified string searching for words within that string contained in a table

I hope I can explain well enough what I am looking for. I tried doing some searches on here, but wasn't finding what I want. I want to do this with strictly mysql if I can with no PHP involved.
So I have a table with terms in it with URL links for each term, we'll call that terms. I have another table with short paragraphs of text we'll call paragraphs. What I want to do is select a paragraph from the table and search it for any of the words contained in the terms table. If the term is in the text, I want to replace it with a concatenated html anchor tag using data from the terms table. I then want the paragraph returned as a formatted string with the anchor tags in place. This way if I add or remove terms to or from the table, I always get a paragraph formatted with the most up to date terms.
Let me know if this is too confusing and I'll see if I can explain it better. Also, if I missed something in my searching that is already posted for this, feel free to guide me in that direction.
Thanks in advance for any help.
-------------------------------------------------
Edit: Here is an example.
-------------------------------------------------
`terms` table
-------------------------------------------------
ID | term | URL
---|----------|----------------------------------
1 | printer | http://t-shooting.link/printers
2 | scanner | http://t-shooting.link/scanners
3 | copier | http://t-shooting.link/copier
-------------------------------------------------
`problems` table
ID | problem
---|---------------------------------------------
1 | the two printers (side by side) need cleaning as the
| copies from them come out with streaks.
2 | My computer won't recognize scanner when I try to use
| it. I was able to use the copier to scan for now, but
| it would be nice to use the scanner that I have in my
| room.
So if I select ID 2 from the problems table I need it to search for any words contained in the terms table and replace them with a mysql "CONCAT('', terms.term, '')" or however I decide to format that. This would make the paragraph look like this:
My computer won't recognize scanner when I try to use it. I was able to use the copier to scan for now, but it would be nice to use the scanner that I have in my room.
The more I do on the mysql side, the less overhead I have on the PHP side. I know ways to do this in PHP, I am just wondering if there is a way to do it in mysql.
Does that help a bit?

Storage - Logic/Performance - PHP/MySQL

Okay, here's a standard users table;
Full Name | Birthday | E-Mail | Username | Password | Facebook | MySpace | Twitter | LinkedIn
Nothing unusual about this, it's fairly standard and text book. However, instead of multiple social networking columns for each network it could be stored like so;
Full Name | Birthday | E-Mail | Username | Password | Social
The difference being the information would be stored in the social column as an imploded array rather than in separate columns. It's quite sensible, so if there are into the thousands of users surely it would be quicker to process via script and less hit on the database.
Can anyone think of any DISADVANTAGES of using the suggested method instead of the text book method?
The two disadvantages I can think of:
It will be more difficult to query for social details specific to a certain user. If you knew their Facebook username was fbuser123 then you might have to query for something like SELECT * FROM users WHERE social LIKE '%fbuser123%'.
It will be slightly more difficult to use the information once it's been selected from the database, for example: Requiring that the field be json_decode'ed before it can be used.
Other than that, I can't think of anything else.
I would imagine that if you did this, the most efficient way of storing the data would be in TEXT format and json_encode the data.

Searching MySQL text for specific number using url paramters

I have a database like this:
id | link | someotherstuff
---------------------------------------------------------
1 | index.php?video=123 | blah
2 | index.php?video=4567&other=variable | blah
3 | site.com/index.php?video=video=1 | blah
and so on.
I would like to use URL parameters to find the right video link.
When I visit: www.mysite.com/index.php?video=1 I want the database to find the "video=1" part of the link column. I have everything working except when searching the link column it finds both id 1 and id 3 since they both have the number 1 in the link.
I'm using Piwik to get statistics for my pages. I use a session to get the video number from the url and then use this:
$videostring = "video%3D".$_SESSION['video']."";
with this:
$resulting = file_get_contents("http://www.site.com/piwik/?module=API&method=Actions.getPageUrls&idSite=1&period=range&date=2013-04-05,today&format=php&expanded=1&token_auth=$token_auth&filter_pattern=". $videostring . "");
How do I use the filter_pattern so I can limit the search to just the number I want?
I think there is no useful way except than using id value in your link address, like site.com/index.php?video?video=1&id=3. So you don't need to search database for finding id.it's in your address
select id from table where link like '%video=1' OR link like '%video=1?%'
With The Edit:
You cannot do that if your server puts % before and after the value of filter_pattern to do filtering.Check for server side code to see what it does to filter_pattern variable just before calling the mysql.

Yii - creating a link from data stored in a table

Ive been trying to think of / find a solution to an issue i have. Im creating a menu system for my Yii app but I need to store part of the items need for creating the URL in a table.
I have a table called system_menu_item:
---------------------------------------------------------------------|
item_id | dis_text | action | variables |
---------------------------------------------------------------------|
1 | edit article | document/view | array('id'=>$model->arl_id) |
What i was hoping for was:
$model = SystemMenuItem::model()->findByPk('1');
$url = yii::app()->createUrl($model->action, $model->variables);
This doesn't work.. I tried different ways of doing it all not working. Any ideas?
Thanks
I'm going to guess the issue is that $model->variables evaluates to a string with the value array('id'=>$mode->arl_id).
An ugly (and dangerous!) hack would be to read this in using eval()
A better way would be to probably figure out a different way to store your parameters, and read them into an array after performing your DB query.

Categories