How could I make my webpage change its content using get parameter? I saw a bunch of website registrations that when you are successfully registered you'll be redirected to the same page but with get parameters i.e www.register.com?do=success, or something like that. I tend to make another webpage for that but this looks promising. Instead of making another page, I would just change the content using get parameter for success registration page.
Nowadays web applications have more or less only one single entry point (index.php) and avoid different and resource specific files. The reason is that a single file provides more control over how the user interacts with the site. The actual resource to be returned to the user is then specified in the query string (or determined through trailing folders and some rewriting mechanism).
A simple approach to establish a single entry point for your entire site:
index.php
<?php
switch ($_GET['pageId']) {
case 0:
echo "Home";
include "content/home.inc.php";
break;
case 1:
echo "Links";
include "content/links.inc.php";
break;
case 2:
echo "Animated Gifs";
include "content/coolstuff.inc.php";
break;
case 3:
echo "Guestbook";
include "content/guestbook.inc.php";
break;
default:
echo "404";
break;
}
Call:
http://localhost/index.php?pageId=0
http://localhost/index.php?pageId=1
http://localhost/index.php?pageId=2
http://localhost/index.php?pageId=3
http://localhost/index.php?pageId=Hohoho
Create an index.php file; in a normal setup that is the file that will be excecuted when calling something like www.yoursite.com/?do=success. The querystring (the part after the ?) parameters can be fetched through the global $_GET array.
W3C: PHP Tutorial
PHP For the Absolute Beginner
PHP: A simple tutorial
Beginners PHP
You just need use the $_GET['do'] to decide what to response to the browser.
Related
Is there a way I can change the header instead of adding on?
For example
Creating a user
create_user.php (a form to action.php) -> action.php (switch case which will call another function) -> user_controller.php
So from user_controller.php, upon successful creation, i used the header method to bring the user to another location, view_user.php.
So now the url header is like
http://localhost/dct/action.php/web/view_user.php?&success=[SUCCESS]%20User%20is%20created!
but i want it to be
http://localhost/dct/web/view_user.php?&success=[SUCCESS]%20User%20is%20created!
without the action.php. How do i go about achieving that?
You will need to use URL rewriting - this depends on your setup. You can either do it at the web-server level, or application-level.
Two ways :
save your "base_url" website (http://localhost/dct/) in parameter and call header('Location: ' . $baseUrl . '/veiw_user.php?...)
Simply add a / before your URL (example: header('Location: /view_user.php?...'), and it will redirect from the root of your website, but in your case, it will redirect to http://localhost/view_user.php?....
The first way is better for your case, but you have to update the parameter when you publish your website in production environment.
This question already has answers here:
Reference: mod_rewrite, URL rewriting and "pretty links" explained
(5 answers)
Closed 8 years ago.
How can i create pages in php, but don't create physical pages. The page don't exist in disk.
I don't want create a physical page in disk for each page ... Image with 3 thousand pages ...
Well, that's really general question. I assume that you're a beginner. Sorry if you're not but that what it seems to me like.
There are like 100 ways of creating website using PHP. PHP is there so you don't have to, like you said, create separate file for each page. If you did, it would be just a simple templating (you create HTML layout and put little PHP scripts inside so you can generate content dynamically).
I believe this isn't right place to ask. Instead you should look up some tutorials, get the picture of how PHP works in general and than maybe try something out and THAN ask again.
W3Schools.com is a great website to start with. Great tutorials, learned a lot there :-)
I think you want to avoid creating physical files on the disk.
The simplest thing comes in my mind, as you can use $_GET.
Take a look at this code.
index.php :
<?php
// Get the "page" information from HTTP GET.
$page = (isset($_GET["page"]) ? $_GET["page"]; : "default";
// Display content according to the page information from GET.
switch($page) {
case "foo_page":
echo "this is foo page.";
break;
case "bar_page":
echo "this is bar page.";
break;
default:
echo "this is the default page.";
break;
}
?>
Now your index.php will respond
this is the default page.
But when you go to index.php?page=foo_page, you'll see that it outputs
this is foo page.
Then you could rewrite it by a .htaccess file.
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_]+)/?$ index.php?page=$1 [NC,L]
Which would make your url's cleaner, like :
domain.com/foo_page/
domain.com/bar_page/
As i've told, this is the first thing i thought of when i read the question.
Hope this helps, or at least gives an idea.
I think you have lot's of images to display in page by page.
Like page1|page2|etccc
if so
First you should plan how it works. In this case , ill do
initialize a variable noofimage=30
another variable start-from=startfrom * finish.
initially start-from=0; // because it start from 0 to noofimage you specified to show in single page.Place this line below nofimage;
do query in php to start from "start-from" to "noofimage"
get total images and divide it by noofimage.
if you have 3000 images then 3000/30=100
so now you have 100 pages.
now using while loop create a page link
int i=1 // start from page 1
while(page<=100)
{
<a href="yoursite.com/images.php?pageno=$i>
i++ // increment page
}
now in your sitebelow
page1|page2|page3|.....
wikk created ....
if some one clicks a page.
at server ,
php files first
$start-from=$_GET['pageno']
$finish= $start-from * $ noofimage;
$start-from=($start-from -1)* $ noofimage
// we are using startfrom- 1 because , in next page images should start from 30 to 60. so we get a page multiple it with noofimages per page for $finish and for start we just decrement 1 then multiple it with noofimage.
now , use php mysql query to (start from , end);
// I just shared a idea how to do php for this page.
try it , or ask if you have any problems.
I have the following code:
<?php
$path=$_SERVER['HTTP_HOST'].$_SERVER[REQUEST_URI];
$test=file_get_contents('http://www.domain.com/test.php?i='.$path);
echo $test;
?>
I want to fetch and insert a specific section of the test.php file depending on the $path variable (the url from which the test.php file is accessed.)
How do I do this? Am I on the right track? I don't know how to set up my text.php file so that the function echos the correct section for the webpage it is accessed from. How do I do this? Or is there a better way? (test.php is not a local file)
Thank you!
It seems to me that you are trying to display dynamic information (such as html or text) on test.php based upon whichever script accesses it. From there, you would like to take the information from test.php and relay it back to index.php by extracting the source code and echoing it? Correct me if I am wrong.
This is achievable using $_GET variables. You were already heading in the right direction by adding the ?i= to the end of the URL.
Something like this should help you roughly achieve what you want:
(test.php)
if (isset($_GET['i]') {
switch ($_GET['i']) {
case 'http://www.domain2.com/index.php':
echo 'You\'re on domain2.com';
break;
case 'http://www.domain3.com/index.php':
echo 'You\'re on domain3.com';
break;
}
}
index.php should work fine how it is, unless the data stored in your variable regarding the path to the page you're currently on is incorrect.
So I made a script so that I can just use includes to get my header, pages, and then footer. And if a file doesnt exist a 404. That all works. Now my issue is how I'm supposed to get the end of the url being the page. For example,
I want to make it so that when someone goes to example.com/home/test, it will automatically just include test.php for example.
Moral of the story. How to some how get the page name. And then use it to "mask" the end of the page so that I don't need to have every URL being something.com/home/?p=home
Heres my code so far.
<?php
include($_SERVER['DOCUMENT_ROOT'].'/home/lib/php/_dc.php');
include($_SERVER['DOCUMENT_ROOT'].'/home/lib/php/_home_fns.php');
$script = $_SERVER['SCRIPT_NAME']; //This returns /home/index.php for example =/
error_reporting(E_ALL);
include($_SERVER['DOCUMENT_ROOT'].'/home/default/header.php');
if($_GET["p"] == 'home' || !isset($_GET["p"])) {
include($_SERVER['DOCUMENT_ROOT'].'/home/pages/home.php');
} else if(file_exists($_SERVER['DOCUMENT_ROOT'].'/home/pages/'.$_GET["p"].'.php')) {
include($_SERVER['DOCUMENT_ROOT'].'/home/pages/'.$_GET["p"].'.php');
} else {
include($_SERVER['DOCUMENT_ROOT'].'/home/default/404.php');
}
include($_SERVER['DOCUMENT_ROOT'].'/home/default/footer.php');
?>
PHP by itself wouldn't be the best choice here unless you want your website littered with empty "redirect" PHP files. I would recommend looking into the Apache server's mod_rewrite module. Here are a couple of guides to get you started. Hope this helps!
The simplest way would be to have an index.php file inside the /home/whatever folder. Then use something like $_SERVER['PHP_SELF'] and extract the name if you want to automate it, or since you are already writing the file yourself, hardcode it into it.
That however looks plain wrong, you should probably look into mod-rewrite if you are up to creating a more complex/serious app.
I would also recommend cakePHP framework that has the whole path-to-controller thing worked out.
I have a website, say accessible under http://example.com.
For this, I have several PHP-scripts like index.php, intro.php, faq.php, contact.php etc.
So a typical use-case would look like so:
User going to http://example.com, which will be http://example.com/index.php -> then clicking on "Introduction" and being redirected to http://example.com/intro.php.
While all this is working nicely, I wondered if there is a way to hide the names of the PHP-scripts completely, so the URL will always read as http://example.com/, regardless whether the user is on index.php, intro.php, faq.php etc.
Using RewriteRules seems not the way to go as it is basically doing the other direction: Facilitating the input of a specific URL for the user (e.g. making the ".php" optional).
However, I want the user to get only the URL of the site to be visible and not the individual scripts along its way.
Is something similar actually possible with individual scripts or would this require all the individual scripts to be combined into one and then to use constructs such as:
if( $_POST['destination'] == "intro" )
{
//DO ALL THE Introduction MARKUP
}
Thank you.
Best.
You could use a full-page iframe, and load intro.php in the iframe. This way, the user stays on the same page, but the page in the iframe changes.
one way (working, but not very good) is to include all your scripts in index.php and call functions which draw specific pages from those scripts. this call s must depend on dome post variable.
You could use AJAX calls to load the new contents when a user clicks on a link. Then you could create your website like usual, but add a script similar to this one (using jQuery):
$(function() {
$('a').click(function() {
$.get($(this).attr('src'), function(data) {
document.write(data);
});
return false;
});
)};
I haven't tried this code, but something along this lines should work.
This would of course not work in browsers that do not support JavaScript, and you would need to take care of forms in another way, so a full-page iframe might be an easier solution.
Given your further explanation, I'd go with the single clean index.php, and other scripts included as needed (I'd even them outside your document root so they can't be accessed directly, either by accident or on purpose):
index.php:
<?php
$action = isset($_POST['action']) ? $_POST['action'] :'index';
switch($action){
case 'intro':
require '../pages/intro.php';
break;
case 'somethingelse':
require '../pages/somethingelse.php';
break;
case 'index':
default:
require '../pages/index.php';
}
?>
Possibly even somewhat optimized with a whitelisted array of possible pages. This keeps your original index.php small & tidy, with still the possibility to do all more complex stuff in dedicated files. No actual need for javascript (it's not needed for the functionality, but of course can be used as desired) or psuedo-hidden urls due to frames (which most of the time doesn't fool a search indexer or someone who just wants to use direct urls with the smallest amount of knowledge about html).