0

I want to get the URL form server side.

I'm using that request to call ajax to server:

$.ajax({
  type: "post",
  url: "includes/pages_includes/func_infos.php",
  data: {
    "...."         : "..."
  },
  beforeSend: function() {

  },
  success: function(data) {

  }
});

If I use $_SERVER['REQUEST_URI'](In func_infos.php) to get URL It gives me: includes/pages_includes/func_infos.php.

I want to get as URL: http://localhost/insc/web_/view_page.php?id_page=10.

How can I get the needed URL?

Vishwanath
  • 6,284
  • 4
  • 38
  • 57
zagbala
  • 7
  • 3

3 Answers3

1

Use this approach for javascript:

var url = window.location.href;

For PHP:

<?php 
// Program to display URL of current page. 

if(isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on') 
    $link = "https"; 
else
    $link = "http"; 

// Here append the common URL characters. 
$link .= "://"; 

// Append the host(domain name, ip) to the URL. 
$link .= $_SERVER['HTTP_HOST']; 

// Append the requested resource location to the URL 
$link .= $_SERVER['REQUEST_URI']; 

// Print the link 
echo $link; 
?> 
Sajad Haibat
  • 697
  • 5
  • 12
1

You can use the following code to current full url using php

$pageURL = 'http';
if ($_SERVER["HTTPS"] == "on") {
    $pageURL .= "s";
}
$pageURL .= "://";
if ($_SERVER["SERVER_PORT"] != "80") {
    $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
} else {
    $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
}

Refernce link : http://webcheatsheet.com/PHP/get_current_page_url.php

Ajith
  • 2,476
  • 2
  • 17
  • 38
0

If you want to get full url or your server supports both http or https then try

$link = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
navi
  • 102
  • 1
  • 11