2

I have a URL alias sitepath/john/video/nature-beauty where john = user, video = content type and nature-beauty = node. I am trying to get nid of node nature-beauty. Node title is "Nature beauty" actually. I have set node title in URL alias.

Can anyone tell me where am I wrong in below code?

   $last_page_visited = $_SERVER['HTTP_REFERER'];
   $alias =$last_page_visited;
   $path = drupal_lookup_path("source", $alias);
   $node = menu_get_object("node", 1, $path);
   $nid=$node->nid;
   print $nid;
0

5 Answers 5

6

The $_SERVER['HTTP_REFERER'] will return the full URL, but the function drupal_lookup_path() takes only the alias as argument, so parse you URL and pass only the alias, john/video/nature-beauty then it will work.

$url = $_SERVER['HTTP_REFERER'];
$path = preg_replace('/\//','',parse_url($url,PHP_URL_PATH),1);
$org_path = drupal_lookup_path("source", $path);
$node = menu_get_object("node", 1, $org_path);
$nid=$node->nid;
print $nid;
6
  • Yes and tried too. But after parsing url its displaying text "array" as parsed url.
    – D-pak
    Commented Jun 25, 2014 at 6:52
  • @D-pak how did you parse?, which function you used
    – msmani
    Commented Jun 25, 2014 at 6:53
  • Here it is $parsed=drupal_parse_url($last_page_visited);
    – D-pak
    Commented Jun 25, 2014 at 6:58
  • 1
    drupal_parse_url() will return an array containing path,query etc. Try preg_replace('/\//','',parse_url($_SERVER['HTTP_REFERER'],PHP_URL_PATH),1);
    – msmani
    Commented Jun 25, 2014 at 7:11
  • @wolverine Please update your answer(by including code) so that it would be more clear for future readers :) Commented Jun 25, 2014 at 9:37
0

There are number for of ways to get nid but its depend how your page is build. If its a direct node page then you can get using $_GET["q"] or using arg() function. You can also use menu_get_object() function directly and get the $nid=$node->nid;.

  • $_GET["q"] would return like node/2345 2345 is your node nid.
  • arg(1) would return node nid.
1
  • After using $_GET["q"] its giving correct answer. But how to do it with last visited page url?
    – D-pak
    Commented Jun 25, 2014 at 6:41
0

There is nothing wrong in your code. Just use drupal_get_path_alias instead of $_SERVER['HTTP_REFERER']; to get url alias.

If you want to use $_SERVER['HTTP_REFERER'] you need to parse the it like follows.

   $path = $_SERVER['HTTP_REFERER'];
  $filter = preg_replace('/\//','',parse_url($path,PHP_URL_PATH),1);
   $path = drupal_lookup_path("source", $filter);
   $node = menu_get_object("node", 1, $path);
   $nid=$node->nid;
   print $nid;
3
  • I am trying to get nid from the previously uploaded node url. And that's why i have written $_SERVER['HTTP_REFERER'];
    – D-pak
    Commented Jun 25, 2014 at 6:09
  • @D-pak Sorry for late response, I've updated my answer Commented Jun 25, 2014 at 7:46
  • Yes I have written same code before and it works fine, Thanks.
    – D-pak
    Commented Jun 25, 2014 at 8:24
0
$path = drupal_get_path_alias($_GET['q']); 
$path = explode('/', $path);
echo $nid = $path[1];

Or simply arg(1) will return the node id of the page By using this, you can get the nid of any node. But it should be the detail page of any node

4
  • How to do it for last visited page url?
    – D-pak
    Commented Jun 25, 2014 at 6:44
  • did you check my answer
    – msmani
    Commented Jun 25, 2014 at 6:48
  • Yes and tried too. But after parsing url its displaying text "array" as parsed url.
    – D-pak
    Commented Jun 25, 2014 at 6:53
  • I have made some modification in the code, its working example Commented Jun 25, 2014 at 14:35
0
global $base_url;
$referrer = substr($_SERVER['HTTP_REFERER'], drupal_strlen($base_url));
$referrer = drupal_get_normal_path($referrer);
$referrer = trim($referrer, '/');
if (arg(0, $referrer) == 'node' && is_numeric(arg(1, $referrer)) {
  $nid = arg(1, $referrer);
}

That's it. $base_url includes subdirectory, if your site is not in server root: http://example.com/drupalsite. drupal_get_normal_path transforms alias into drupal internal path, that is, article/marvelous into node/101. It is actually a wrapper around drupal_lookup_path, so the use of the latter is discouraged. And then you get your nid.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.