开发者

Change page dynamically

开发者 https://www.devze.com 2023-02-09 06:49 出处:网络
<?php $go = $_GET[\'go\']; if(!empty($go)) { if(is_file(\"page/$go.html\")) include \"page/$go.html\";
<?php

  $go = $_GET['go'];


  if(!empty($go)) {

    if(is_file("page/$go.html")) include "page/$go.html";

    else echo "<h1>Error 404</h1><p>Strona nie odnaleziona</p><p>Warunek ?go jest niepoprawny</p>";
  }

  else include "page/start.html";

  ?>
<script>
$.get('go', function(change) {
  $('#center').load('page/<?php echo $go ?>.html');
});
</script>

I have somethng like 开发者_如何学编程this but it doesn't work. I just want that the page which is loaded (?go=name) won't refresh whole page


Firstly, that's vulnerable to an LFI vulnerability (Local File Inclusion). Consider what happens when someone enters: http://site.com/file.php?go=../../../../../../../../etc/passwd%00

Also, you can't just echo your filename and expect it to print it out...if you want to include the contents of the page in $go and print out $go, then use:

$go = urldecode([$_GET['go']);
$go = str_replace("/", "", $go);
$go = "page/$go.html";

EDIT: Actually, if you use my above code, they could still access files in the local directory, such as .htpasswd and .htaccess, so just don't let your users include any local files. There are better ways to solve the problem you have.

As for the AJAX, follow jeroen's advice.


You are mixing two ajax functions, $.get and .load and you are missing an event handler.

You will need something like this:

$('#go').live('change', function() {
  $('#center').load('page/' + $(this).val() + '.html');
});

I have used live instead of change in case the go button is located in the refreshed section of the page.

Note that I am guessing that you want to refresh a section of your page based on a selection, the question / code isn´t exactly clear about that...


Is this what you want to achieve?

<script type="text/javascript">
    $(document).ready(function() {
        var getgo = '<?php echo $_GET['go']; ?>';
        if(getgo.length) {
            $('#center').load('page/'+getgo+'.html');
        }
    });
</script>
<div id="center"></div>

It will load the contents of page/[your go param].html into #center.

0

精彩评论

暂无评论...
验证码 换一张
取 消