0

I'm currently learning PHP and from what I'm seeing $_SERVER ['SCRIPT_NAME']; contains the current script's path. However, any page I try to identify shows up as "/index.php".

For example, I will run this on about.php, but the results return "/index.php".

<?php
    $current_file = $_SERVER['SCRIPT_NAME'];
    echo $current_file;
?>

Obviously I'm missing something, but I don't see what it is.

4
  • Yes, you are missing something, but I can't tell what from your information. Of course it always shows the called script, that is the name given in the URL. Commented Apr 4, 2014 at 18:41
  • is about.php a template file? Commented Apr 4, 2014 at 18:42
  • yes, it is a template file Commented Apr 4, 2014 at 18:55
  • @ProfessorB that is why. it is being included into index.php Commented Apr 5, 2014 at 16:14

2 Answers 2

1

Since your about.php is a template file that is at some point included in index.php, that is why you are getting /index.php as a result.

You should try using __FILE__:

The full path and filename of the file. If used inside an include, the name of the included file is returned. Since PHP 4.0.2, __FILE__ always contains an absolute path with symlinks resolved whereas in older versions it contained relative path under some circumstances.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for your response, decker. The reason I am using is to have a form direct back to the page it was on once completed. <form action="$current_file" method="POST"> with $current_file = (__FILE__) returns a url of the full path. Is there another way to return the user back to the original page without using the actual url? NOTE: This is for a login form, so I am giving users access to login on any page.
you could try action="<?=($_SERVER['PHP_SELF'])?>" or action="?" for the form's action attribute. Also, action="" works but I don't think it is valid HTML5.
Thanks, no action necessary (I did not know that). However, that did initially give me an issue - without the action, after signing in I had to refresh the page to get rid of the form. I simply did a header redirect to the referrer page upon complete and viola! Thanks again!
0

'SCRIPT_NAME' Contains the current script's path. This is useful for pages which need to point to themselves. The __FILE__ constant contains the full path and filename of the current (i.e. included) file.

Excerpt from http://www.php.net/manual/en/reserved.variables.server.php

The php.net website documentation is very verbose with some excellent user comments. I would suggest that be your first stop for almost any question you have about php.

2 Comments

You may want PHP_SELF 'PHP_SELF' The filename of the currently executing script, relative to the document root. For instance, $_SERVER['PHP_SELF'] in a script at the address http://example.com/foo/bar.php would be /foo/bar.php. The __FILE__ constant contains the full path and filename of the current (i.e. included) file. If PHP is running as a command-line processor this variable contains the script name since PHP 4.3.0. Previously it was not available.
Thanks for your response. That's actually been the resource I've been using. I have tried PHP_SELF but still get the same results

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.