更改网页的内容而不更改URL [英] Change the content of web page without changing URL

查看:93
本文介绍了更改网页的内容而不更改URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想制作一个简单的网站,用户可以输入他们的名字。但我不知道如何做到这一点。



这就是我想要的。



在我的 index.php ,会有一个输入,允许用户输入他们的名字。用户点击一个按钮,我希望用户留在 index.php 但内容不同。
我可以做网站的文本框和按钮。但我不知道在用户点击按钮后应该怎么做。



不明白我的意思吗?
例如,这是Facebook登录页面。



in,





我们仍处于同一页面(index.php),但内容已更改。我知道如何制作网站,但我不知道如何在不更改网址的情况下更改内容(例如www.example.com/index.php?u=blah,我只想要www.example.com/index.php)

任何人都可以为我提供一个关于如何做到这一点的示例/想法?先谢谢了。
对不起,我的英语不好。



编辑:对不起,我的英文不好。其实我的意思是,同一个网址但内容不同。我搜索了一下,我认为 cookie 是最好的解决方案。

可以使用PHP来做到这一点,这是我为我的所有网站做的这只是一个想法,不知道它是否正是你正在寻找或不,但这是一个链接到我目前的网站,如果你想现场看到它。请务必在点击网站顶部的链接时检查网址。 http://www.thedecoy.net



所以在你的index.php页面中,你将拥有你的基本html代码,包括css和图片,或者你在站点上使用的任何你想在所有站点上显示的东西,然后在里面说你的内容div标签,你可以做到这一点

 <?php 
error_reporting(E_ALL ^ E_NOTICE);
函数open_page($ string){
if(preg_match(/ ^ [a-zA-Z] + $ /,$ string)){
$ string = $ string;
} else {
$ string =;
}
return $ string;
}
$ page = open_page($ _ GET ['page']);
if($ page){
$ file =pages /\".$ page。。php;
if(file_exists($ file)){
$ pagename = $ page;
} else {
$ pagename =404;
}
include(pages /\".$ pagename。。php);
} else {
echo'你想要显示在你的主页内容中的任何东西都会在这里出现';
}
?>

您可以在index.php文件所在的根文件夹中创建一个名为pages的目录,然后在里面你可以创建其他页面,比如about.php,contact.php等。我相信你会明白,在这些文件中,你只会放置你想要显示的内容,所以你不需要所有的HTML头体或者任何东西只要你想在内容div本身。



假设你想链接访问你的网站的用户到contact.php文件,你只需要调用链接所以。

 < a href =index.php?page = contact>联系人< / a> 

这将保持index.php加载,但将内容更改为contact.php信息。
这段代码有点sl and,从我的网站本身拉出来,所以你可能需要处理一些事情。



在页面中放置一个404.php页面如果没有找到页面,这个文件夹将会被调用,所以如果访问者输入一个不存在的链接,访问者就会看到这个页面。



不知道我是否遗漏了任何东西但请随时询问这是否有助于您。


I want to make a simple website where a user can enter their name. But I have no idea how I can do this.

Here is what I want.

In my index.php, there will be an input which allows the user to type their name in. After the user click a button, I want the user to stay in index.php but with different contents. I can do the website which textfield and button. But I don't know what should I do after the user clicks the button.

Don't get what I mean? For example, this is the Facebook login page.

After logged in,

We are still at the same page(index.php) but the content changed. I know how to make websites, but I don't know how to change the content without changing URL(e.g. www.example.com/index.php?u=blah, I only want www.example.com/index.php)

Can anyone provide me an example/idea about how do I make this? Thanks in advanced. Sorry for my bad English.

EDIT: Sorry for my bad English. Actually I mean, same URL but different content. I googled, I think cookie is the best solution.

解决方案

You could use PHP to do this as well it is what I do for all of my websites this is just an idea not sure if it is exactly what you are looking for or not but here is a link to my current site if you would like to see it live. be sure to check the url as you click the links at the top of the site. http://www.thedecoy.net

So inside your index.php page you would have your basic html code for the site including the css and images or anything else you use on the site that you want to display on all sites then inside of lets say you content div tag you could do this type of php code.

<?php 
error_reporting(E_ALL ^ E_NOTICE); 
function open_page($string){
    if (preg_match("/^[a-zA-Z]+$/", $string)){
        $string = $string;
    }else{ 
        $string = "";
    }
    return $string;
}
$page = open_page($_GET['page']);
if ($page){
    $file = "pages/".$page.".php";
if (file_exists($file)){
    $pagename = $page;
}else{
    $pagename = "404";
}
include("pages/".$pagename.".php");
}else{
    echo 'Anything you want displayed in the content of your main page would go here';
}
?>

You would make a directory called "pages" in your root folder where the index.php file is and then inside this you could create other pages such as about.php, contact.php ect I am sure you get the point, In these files you would only put the content you would want displayed so you would not need all the html head body or anything just what you want in the content div itself.

Let say you wanted to link users visiting your site to the contact.php file you would simply call on the link like so.

<a href="index.php?page=contact">Contact</a>

which will keep the index.php loaded but change the content to the contact.php information. The code is a bit sloppy and pulled from my site itself so you may have to work with some things.

Put a 404.php page in the pages folder this will be called if a page is not found so a visitor will see this page if they enter a link that does not exist.

Not sure if I left anything out but please feel free to ask if this helps you.

这篇关于更改网页的内容而不更改URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆