0

when the url is localhost/school/portfolio/contact.php it works fine, but when I use a get parameter index.php?page=contact it redirects to index.php

The code I use

$url = "http". ((!empty($_SERVER['HTTPS'])) ? "s" : "") . "://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
        header('Location: '.$url."?sent=yes");

What do I have to add?

3 Answers3

2

This should help :)

<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post">

Otherwise see my answer on this "How to get base URL with PHP?" Question for more help :)

Community
  • 1
  • 1
maček
  • 76,434
  • 37
  • 167
  • 198
0

there are couple of ways to do so.. (choose what suits you..)
1)use $_SERVER['PHP_SELF']
2)Use parse_url() to get PHP_URL_PATH and PHP_URL_QUERY parts, and redirect to relative path.
But it'll better to store redirect links in session.

Taimur Amjad
  • 392
  • 2
  • 14
0

This seems to be an old thread, but to help the future visitors to this page it is worth adding a comment about security issues with $_SERVER['PHP_SELF'].

If adding $_SERVER['PHP_SELF'] to the form action attribute as suggested in the comments above, you are setting yourself up for XSS attack.

One solution is to wrap $_SERVER['PHP_SELF'] with htmlentities() like so:

<form action="<?php echo htmlentities($_SERVER['PHP_SELF']) ?>" method="post">

This is a good read on the topic:

http://www.html-form-guide.com/php-form/php-form-action-self.html

MasterAM
  • 16,283
  • 6
  • 45
  • 66