So let's say I have my registered domain hosted on Apache 2. Right.
Let's say
cool-domain.com
Now, I make a script which has the functionality to show all my users the same kind of background and frame and stuff, but each one gets a different picture of a cat or a dog.
So, if my user wants to see a dalmatian, he would enter:
cool-domain.com/dogs/dalmatian
Right, and if my user wants to see an orange cat, he would enter the following URL:
cool-domain.com/cats/orange
Now, I don't want to make a different HTML page for each cat and each dog. I want one html which changes the image source according to the URL.
So my PHP backend would remeber something among the lines of:
$species = "cats";
$type = "orange";
and would check these through the database.
But even if I go to "cool-domain.com" I still go to the same HTML page but the variables are left NULL.
Related
I am trying to figure out the best way/how to go about this -
I am relatively new to mysql and PHP and need to create user profile pages that display different values from a mysql database that have been uploaded for each user -
for example if I clicked on John's profile, the page would display John's name, age, any other values that I have stored on John in my mysql table. This is from the perspective of someone else logged in who wants to view John's page.
Since all my index.php files for my different web pages are all in their own folders (i.e. the about page, settings page, etc) I originally thought to simply have a user profile folder and index.php that just outputs the right information in accordance with the $user_id of the user that was clicked. That is the only way I can think to accomplish this.
My problem is that this would not allow for a person to access a user profile via URL (i.e. website.com/user1) and I am not sure my approach is the best/correct way to create user profile pages. I have explored this question and read up on URL rewriting but am still unclear -
What is the correct way to create separate user profile web pages and how does URL rewriting tie into the actual contents of the page's index file?
Is there a way to (this is poorly worded) store a separate index.php file for each user within their existing mysql column and just direct to this file when clicked?
Hard to answer without knowing how the structure of your application is looking.
But the most common way, and the most simple implementation would be to:
create a profile.php (profile-action in user-controller or whatever..)
add the user-id to the url, yourapplication/profile.php?userid=12
In your profile.php you simply fetch the data which belongs to the given userid (first validate the id etc...)
fill your html with the fetched data
So in short: create a view, fill in dynamic data depending on the userinput
I don't think what you want to do is related to URL rewriting. URL rewriting is simply writing facebook.com/user1 instead of facebook.com/user1/index.php (or whatsoever).
"My problem is that this would not allow for a person to access a user profile via URL" I believe this part is incorrect. Someone who logged in as, say, user1 can view the page www.website.com/user2 and see another person's profile.
Check out this website to get a better understanding for URL rewriting. Facebook uses it as you can access a person's profile from facebook.com/user5. On the other hand, one comment suggested you to use dynamic php such as /profile.php?id=5. It is your choice what to pick. But the first option does not prevent users to check out others' profile pages on the contrary to what -I believe- you assumed.
I'm not even 100% sure how to ask this question, but I will try my best...
So, take youtube. You've got this:
URL/watch?v=Video_URL_Here
While on this video, you decide to click a video in the related on the right-side.
While doing that, the page refreshes, and instantly jumps to that video.
I have the basic concept down:
> Create a variable.
$var;
> User: *Clicks First Video*
$var = Video_One; // Pulls from mySQL-DB
> Open a new page (ex: URL/watch?v=Video_ONE)
PHP: >Creates a whole new page for the video.<
> User: *clicks new video*
$var = Video_Two;
> Open a new page (ex: URL/watch?v=Video_TWO)
PHP: >Working more magic.<
However, I'm having a hard time actually doing this.
Could anyone point me in the right direction or explain how it works?
It would be very appreciated.
The way YouTube works is using $_GET variables. That's what the ?v= is. It's taking in the v variable and checking the database for a video with that video id. The way they create the new page is by fetching each of the values corresponding to the id that was passed in the url, then putting that data in each of the page sections.
Let me answer with a very general and oversimplified example
actually, more than having a unique "$var" that changes every time you click on a video (on your example), it is more like the page already knows where to go for each link (or click), that is, every video already has a link associated, with the corresponding url.
all this is done BEFORE the page loads. (there are ways to make it after, but that is another matter).
Just to give a quick example (it may not be exactly how youtube works, it is just an example)
Lets say you store each videos name, description, rating, etc on a database table.
e.g.
video1name, url1, description1, etc1
video2name, url2, description2, etc2
video3name, url3, description3, etc3
also assume each video has already related videos stored somewhere (the videos which would show on the right side) (imagine they are in the same table, each video having their own "related videos" associated.
so, when putting the page together, via PHP (in this case), what the code does is, read the data from the database, so it will know what it will "paint", at that point, it stores such data in variables, and using those vairables, it is ready to build the page, using such data.
imagine you say "i need 5 videos here, those videos are this, this other .... etc"
so php will read those 5 videos info form the database, AND knowing their data, it already "knows" what will the specific url for each video will be.
it only has to build links for each video, each having it's speciffic url.
e.g.
[some html]
...
<a href="myvid1url" > ...</a>
<a href="myvid2url" > ...</a>
<a href="myvid2url" > ...</a>
...
[the rest of html]
the only thing php is doing, is creating HTML dynamically, based on that data, and once it finishes, it sends it to the browser, which only has to "paint" plain html, all of which is already filled with the particular urls, names, etc for each part.
This is a VERY generalized example, but i hope you get the idea.
The most important part is to understand that most of the time, pages are already "built" before being displayed, once loaded, they already "know" what to do when you click somewhere, etc.
Of course, you can add interactive functionality, using javascript, ajax, etc, and that MAY change the page already loaded, but that is another concept.
I think you should first tell us what your experience with programming is, or if you have only made plain simple htmls pages or anything, so we could give you better advice.
have fun!
You could use JQuery and have the second video load in a frame, iframe, div, table, new window, etc (depending on the data source, of course)
External sources (depending)
jQuery loading external page (cross domain) into Div element
Local content sources
Load HTML page dynamically into div with jQuery
For external data loading you could get all creative and run a curl to save the data locally, parse it for what you need and then serve that locally
I know this is a strange thing to want to do, but...
How can I place a photo at the top of someone else's webpage ("Page B") -only- when a visitor links there from my page ("Page A")? I have permission to do this, but with certain limitations: my code needs to be in one location on Page B, and I can't make major changes to Page B. I cannot use Page B's server to run PHP, but I can run PHP on Page A's server.
The photo needs to open automatically with the rest of the Page B, and not require any user input to show up.
This is a unique vendor/retailer situation, so unfortunately I can't provide the urls.
Thanks for any help.
In PHP, you'd do something like:
<?php
if($_SERVER['HTTP_REFERER'] == 'http://www.yoursite.com/referringpage.html')
{
printf('<img src="yourbutton.png">');
}
?>
Which is very limited because:
It will only work for a referring single page (referringpage.html) and not otherreferringpage.html
It will only work for a single landing page. If the user navigates off of the page, then back (not through browser's back), the button will not show up.
To solve the first you could do a substring of the referrer, e.g.
<?php
if(substring($_SERVER['HTTP_REFERER'],0,24) == 'http://www.yoursite.com/')
{
printf('<img src="yourbutton.png">');
}
?>
To solve the second you could set session variables (except then you'd need access to the very top of the "outermost" page -- which doesn't sound like an option.)
Depends on the design of the page.
Lets say,
If the page is divided into frames, then you can use iframe.
if It's a single page, try have a separation with div tag. Insert the html into Div Tag.
If the above two points won't help you,give an idea of how the page is designed
I think a simple answer would be to write a html page (lets call it linkPage) that has the image/text/div or whatever you want to display on the top and then include an iframe that would cover the rest of the page using css. You could pass the page you are linking to as a GET or POST parameter to the linkPage and then set the src of the iframe to that url.
There are probably other ways of doing this that are more complicated / seamless but this would be the easiest, quickest way to get it done.
I always try to make code nicer and more maintainable but I'm not sure if I do it right... For example, I'm doing a social network site, so the page domain.com/profile?id=17 would open profile for user id 17. If the user is the currently logged in one, then the profile will be seen as the owner (with edit options, etc) otherwise the page would show a profile for outsiders (no edit options).
How do I do this nicely? I was thinking that after checking if the user id == the user id in $_SESSION, I include one php file (the one with the edit options), otherwise I include the other one. Is this a solution? Is there a better one?
I do the same thing for headers, I have a file 'header.php' that includes the header of all pages (some php but also a logo, etc), however I'm not sure if this is good since a user can go to his browser and type mydomain.com/header.php and then be displayed a header only.
Your solution is one way to do it .another way to do it would e just check the user_id in session and if it is not set then just display another html . Also you would want to access users as their usernames rather than user id.For example instead of user_id.php=17 to username/luqita.If you are using apache as the web server then you could use it's mod_rewite module to generate pretty urls.
I have a large, PHP-based CMS that manages web pages.
All items are organized in a tree structure.
When I edit an item, the "back" button usually points to its parent item.
So, the usual workflow is navigating through the tree.
Now every now and then, the need arises for a workflow that "jumps" to other items without regard for the structure.
For example, when a user is editing a web page, they may want to open the template the page is attached to (another item in a completely different branch), make a change there, and when clicking "save" expect to come back to the page they were editing.
At the moment, I solve this using
domain.com/admin/template/edit?from=/frontpage/edit
where the "from" variable determines the target URLs of the "save" and "cancel" buttons.
This works up to a certain point when the path becomes too long and complex. For example, what if the user
edits a page
opens the attached template
previews that template in the front-end view
and then expects to be seamlessly taken back to the page they were editing?
Right now, the "history" ends at the last item so that when the user returns from the front-end view, the link to the original page is lost, and they have to search it by hand.
Another problem that can happen quickly is that the GET URL containing all the "from" values becomes too long, or totally chaotic:
domain.com/admin/template/edit?from=/frontpage/edit&from=/somepage/edit
&from=/template/preview&/from=template/edit&/from=template_preview ...
(you get the drift)
I have solved this elegantly by opening separate windows in the past, but I really want to implement a seamless one-window workflow that works universally, mainly because multiple windows tend to confuse users.
How do you solve this?
Have you implemented a robust "unstructured" navigation that works well with multiple windows open (=one user doing multiple different things with different navigation paths)?
How do you go about this on the user interface side?
The best approach I can think of is passing on a "from" value that points to a temporary record in a database or session. That record contains all the information about the current path, and can thus always provide the right "back to page x" value.
What I would like to hear most is experiences from people who have successfully implemented this, and how they did it.
Just a couple suggestions
Preview Problem: preview in an IFRAME, so the history doean't get lost?
Cluttered URL problem: If you have some sort of key for each page, other than the URL path
(i.e. /frontpage/edit = 952,
/frontpage/edit&from=/somepage/edit = 763,
/template/preview = 651,
template/edit = 612,
template_preview = 866 etc.)
you could string them together in the PATH_INFO like so:
domain.com/admin/template/edit/952/763/651/612/866
You could build a stack of visited pages for the session as the user clicks around, pushing on a new page each time the open it, popping off when they click back. Store it as a session variable.
The trick then is to always check their referrer string, since they may also use their browser's back and forward buttons. If their referrer string isn't at the top of their stack, you'll have to scan the stack for it to see if they manually backed up to a previous page.
As long as you're only needing to backstep once, why not pass in whatever linkback page IDs you want whenever you produce the page you're jumping to?