PHP Program to show current page URL

URL Functions

Note that $_SERVER[«HTTP_REFERER»] may not include GET data that was included in the referring address, depending on the browser. So if you rely on GET variables to generate a page, it’s not a good idea to use HTTP_REFERER to smoothly «bounce» someone back to the page he/she came from.

just a side note to the above you will need to add the ?

Note also that the URL shown in $HTTP_REFERER is not always the URL of the web page where the user clicked to invoke the PHP script.
This may instead be a document of your own web site, which contains an HTML element whose one attribute references the script. Note also that the current page fragment (#anchor) may be transmitted or not with the URL, depending on the browser.
Examples:

In such case, browsers should transmit the URL of the container document, but some still persist in using the previous document in the browser history, and this could cause a different $HTTP_REFERER value be sent when the user comes back to the document referencing your script. If you wanna be sure that the actual current document or previous document in the history is sent, use client-side JavaScript to send it to your script:

And then check the value of $js in your page script to generate appropriate content when the remote user agent does not support client-side scripts (such as most index/scan robots, some old or special simplified browsers, or browsers with JavaScript disabled by their users).

Читайте также:  Python вывести два графика рядом

Following method do not show the URL in user browser (as the author claimed) if the code resides in the source page of FRAME or IFRAME (say SRC=»sourcepage.php») . In that case the URL of the SOURCE page is displayed.

$url = sprintf(«%s%s%s»,»http://»,$HTTP_HOST,$REQUEST_URI);
echo «$url»;

To check if a URL is valid, try to fopen() it. If fopen() results an error (returns false), then PHP cannot open the URL you asked. This is usually because it is not valid.

When using a multiple select on a form, I ran into a little issue of only receiving the last value form the select box.
I had a select box named organization_id with two values (92 and 93).
To get the values of both, I had to use the following:

$temp_array = split(«&», $_SERVER[‘QUERY_STRING’]);
foreach($temp_array as $key=>$value) if(substr($value, 0, 15) == «organization_id») $_GET[‘organizations’][] = substr($value, 15, strlen($value));
>
>

this results in a $_GET array like this :

(
[page] => idea_submission
[organization_id] => 93
[organizations] => Array
(
[0] => =92
[1] => =93
)

Источник

Как получить текущий URL в PHP?

Сформировать текущий адрес страницы можно с помощью элементов массива $_SERVER. Рассмотрим на примере URL:

Полный URL

$url = ((!empty($_SERVER['HTTPS'])) ? 'https' : 'http') . '://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; echo $url;

Результат:

https://example.com/category/page?sort=asc

URL без GET-параметров

$url = ((!empty($_SERVER['HTTPS'])) ? 'https' : 'http') . '://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; $url = explode('?', $url); $url = $url[0]; echo $url;

Результат:

https://example.com/category/page

Основной путь и GET-параметры

$url = $_SERVER['REQUEST_URI']; echo $url;

Результат:

Только основной путь

$url = $_SERVER['REQUEST_URI']; $url = explode('?', $url); $url = $url[0]; echo $url;

Результат:

Только GET-параметры

Результат:

Для преобразования строки с GET-параметрами в ассоциативный массив можно применить функцию parse_str() .

parse_str('sort=asc&page=2&brand=rich', $get); print_r($get);

Результат:

Array ( [sort] => asc [page] => 2 [brand] => rich )

Комментарии 2

Авторизуйтесь, чтобы добавить комментарий.

Другие публикации

Чтение Google таблиц в PHP

Как получить данные из Google spreadsheets в виде массива PHP? Очень просто, Google docs позволяет экспортировать лист в формате CSV, главное чтобы файл был в общем доступе.

Сортировка массивов

В продолжении темы работы с массивами поговорим о типичной задаче – их сортировке. Для ее выполнения в PHP существует множество функций, их подробное описание можно посмотреть на php.net, рассмотрим.

Источник

How to Get Current Page URL, Domain, Query String in PHP

In this tutorial, We would love to share with you, How to get current page URL in PHP, PHP get current URL path, PHP get full URL with parameters, PHP get a current page, PHP get current URL with query string, PHP get a domain from URL.

How to Get Current Page URL in PHP

Here you will discuss about superglobal variable $_SERVER, this is built-in PHP variable. We will use this variable get the current page URL in PHP. You can get every piece of information about the current URL using the $_SERVER superglobal.

Читайте также:  Python exe file version

Before we try to fetch the Current Page Full URL lets see how a normal URL structure looks like:

http://www.abc.com/dir1/test.php?glob=hello&name=world

Any typical URL like this can be broken into several common parts like:

  • HTTP: The URL protocol
  • www.abc.com – The domain name or the hostname.
  • dir1: The directory within the root
  • test.php – The actual PHP script
  • glob=hello – the first URL parameter (glob) and it’s a value (hello)
  • name=world – the second URL parameter (name) and its value (world)

Now we will create a program that is used to get current page url.

PHP Program for get Current Page Url

Program 1 full source Code

     

PHP Program to show current page URL

PHP Program show Current page full URL

Program 2 full source Code

     

PHP Program show Current page full URL

PHP Program show Current full URL with Query String

Program 3 full source Code

     

PHP Program show Current page full URL with Query String

Other Important elements of $_SERVER that are very are useful and you must know that:

  • $_SERVER[‘SERVER_ADDR’]: IP address of the server
  • $_SERVER[‘REQUEST_METHOD’]: Returns the page access method used i.e. ‘GET’, ‘HEAD’, ‘POST’, ‘PUT’.
  • $_SERVER[‘REQUEST_TIME’]: timestamp of the start of the request.
  • $_SERVER[‘HTTP_REFERER’]: returns the referrer page uri – used in redirecting to last page after login
  • $_SERVER[‘SCRIPT_FILENAME’]: returns the path including the filename, like DIR
  • $_SERVER[‘HTTP_COOKIE’]. returns the raw value of the ‘Cookie’ header sent by the user agent.
  • $_SERVER[‘HTTP_ACCEPT_LANGUAGE’]): returns the default set language – useful for websites with multilingual content & readers
  • $_SERVER[‘HTTP_USER_AGENT’]: returns the kind of device being used to access (desktop/mobile device etc) – suitable for switching interface for different devices.

Author Admin

My name is Devendra Dode. I am a full-stack developer, entrepreneur, and owner of Tutsmake.com. I like writing tutorials and tips that can help other developers. I share tutorials of PHP, Python, Javascript, JQuery, Laravel, Livewire, Codeigniter, Node JS, Express JS, Vue JS, Angular JS, React Js, MySQL, MongoDB, REST APIs, Windows, Xampp, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL and Bootstrap from a starting stage. As well as demo example.

Источник

Php address of page

Learn Latest Tutorials

Splunk tutorial

SPSS tutorial

Swagger tutorial

T-SQL tutorial

Tumblr tutorial

React tutorial

Regex tutorial

Reinforcement learning tutorial

R Programming tutorial

RxJS tutorial

React Native tutorial

Python Design Patterns

Python Pillow tutorial

Python Turtle tutorial

Keras tutorial

Preparation

Aptitude

Logical Reasoning

Verbal Ability

Company Interview Questions

Artificial Intelligence

AWS Tutorial

Selenium tutorial

Cloud Computing

Hadoop tutorial

ReactJS Tutorial

Data Science Tutorial

Angular 7 Tutorial

Blockchain Tutorial

Git Tutorial

Machine Learning Tutorial

DevOps Tutorial

B.Tech / MCA

DBMS tutorial

Data Structures tutorial

DAA tutorial

Operating System

Computer Network tutorial

Compiler Design tutorial

Computer Organization and Architecture

Discrete Mathematics Tutorial

Ethical Hacking

Computer Graphics Tutorial

Software Engineering

html tutorial

Cyber Security tutorial

Automata Tutorial

C Language tutorial

C++ tutorial

Java tutorial

.Net Framework tutorial

Python tutorial

List of Programs

Control Systems tutorial

Data Mining Tutorial

Data Warehouse Tutorial

Javatpoint Services

JavaTpoint offers too many high quality services. Mail us on h[email protected], to get more information about given services.

  • Website Designing
  • Website Development
  • Java Development
  • PHP Development
  • WordPress
  • Graphic Designing
  • Logo
  • Digital Marketing
  • On Page and Off Page SEO
  • PPC
  • Content Development
  • Corporate Training
  • Classroom and Online Training
  • Data Entry
Читайте также:  Открыть html в гугле

Training For College Campus

JavaTpoint offers college campus training on Core Java, Advance Java, .Net, Android, Hadoop, PHP, Web Technology and Python. Please mail your requirement at [email protected].
Duration: 1 week to 2 week

Like/Subscribe us for latest updates or newsletter RSS Feed Subscribe to Get Email Alerts Facebook Page Twitter Page YouTube Blog Page

Источник

Get the Current URL of Webpage in PHP

If you are browsing a webpage or a web application, PHP will store a lot of useful information in the background. This information gets stored in PHP’s super-global variables. These are pre-defined variables that are available in all types of scope. You can use these variables to obtain the current URL of the web page.

How to get the current full URL?

The super-global variable called $_SERVER can fetch you the current URL of a website. Some of the properties of the variable that you can access include HTTP_USER_AGENT , HTTP_HOST and HTTP_ACCEPT . For the URL, we will access the HTTP_HOST and REQUEST_URl property.

But you need to check the protocol of the website, if it is HTTP or HTTPS .

Example Code

Output

https://www.stechies.com/current-url.php?v=1

Explanation

In the code above, the super-global variable $_SERVER is used to access the property ‘HTTPS’ to check and fetch the protocol used for the site. The value is assigned to a variable called $protocol.

Then, the $_SERVER variable is used again to access the HTTP_HOST’ property to obtain the host name. The property REQUEST_URl is used to fetch URL of the current webpage. Both the information is appended using the concatenation operator (.) and stored in the variable $finalurl . This value is printed out using the echo statement.

Function to display full URL

Output

https://www.stechies.com/current-url.php?o=p

Explanation

This code checks whether the current site runs on HTTP or HTTPS , using the $_SERVER . Then, with that value (which can be HTTPS or HTTP), the current hostname and URL of the site is appended. The combined value of all that information gives us the full URL of the website.

Output

https://www.stechies.com/current-url.php?o=p

You can use this code to get the current URL of the webpage when you know that your site is running on HTTP or HTTPS .

  • Learn PHP Language
  • PHP Interview Questions and Answers
  • PHP Training Tutorials for Beginners
  • Display Pdf/Word Document in Browser Using PHP
  • Call PHP Function from JavaScript
  • Call a JavaScript Function from PHP
  • PHP Pagination
  • Alert Box in PHP
  • Php Count Function
  • PHP Filter_var ()
  • PHP array_push Function
  • strpos in PHP
  • PHP in_array Function
  • PHP strtotime() function
  • PHP array_merge() Function
  • explode() in PHP
  • implode() in PHP
  • PHP array_map()

Источник

Оцените статью