2013-04-04 01:47
浏览 98

PHP localhost重定向问题

This is my first question on SO, I hope I'm doing it right. I'm new to PHP/MySQL programming. I've just recently set up a local development environment with WAMP and it is more or less working (at least, I get the green icon, the WAMP php info screen, phpMyAdmin works, and some of my .php files work properly, pulling .css and .inc.php files from the appropriate directories.)

Where I'm having trouble is with php redirect with header().

I've tried many different options, some gleaned from here, but none work. I'm getting an error "The requested URL /localhost/home.php was not found on this server."

Also, in the location bar of the browser I get: localhost/localhost/home.php, but I've no idea why.

Here's one version of the snippet from the calling program (index.php), part of which I got from an answer here on SO:

if (isset($_SESSION['Authenticated'])) {
    $host  = $_SERVER['HTTP_HOST'];
    $uri   = rtrim(dirname($_SERVER['PHP_SELF']), '/\\');
    $extra = 'home.php';
    header("Location: http://$host$uri/$extra");

Original version, also not working, was:

if (!isset($_SESSION['Authenticated'])) {
    $redirect = 'home.php';
    header("Location: $redirect");

I've also tried numerous values for the $redirect variable in the original version, including various versions of the path, to no avail.

As far as my setup goes, here is some info you might find useful:

WAMP server version 2.2 Apache version 2.2.22 PHP version 5.4.3 MySQL version 5.5.24

Relevant line from C:\Windows\System32\Drivers\hosts           bke.local       #BKE Test Environment

I'm using the default C:\wamp\www as the DocumentRoot

Relevant lines from C:\wamp\bin\apache\apache2.2.22\conf\httpd.conf:
# DocumentRoot: The directory out of which you will serve your
# documents. By default, all requests are taken from this directory, but
# symbolic links and aliases may be used to point to other locations.
DocumentRoot "c:/wamp/www/"
# Local access to the Apache HTTP Server Manual
#Include conf/extra/httpd-manual.conf

Relevant lines from C:\wamp\bin\apache\apache2.2.22\conf\extra\httpd.vhosts.conf:
# Use name-based virtual hosting.
NameVirtualHost *:80
# VirtualHost example:
# Almost any Apache directive may go into a VirtualHost container.
# The first VirtualHost section is used for all requests that do not
# match a ServerName or ServerAlias in any <VirtualHost> block.
<VirtualHost *:80>
    DocumentRoot "c:/apache2/docs/"
    ErrorLog "logs/"
    CustomLog "logs/" common

<VirtualHost *:80>
    DocumentRoot "c:/apache2/docs/"
    ErrorLog "logs/"
    CustomLog "logs/" common

#<Directory C:/Dev>
#   Order Deny,Allow
#   Allow from all

Current locations of my files: .php files into C:\wamp\www .css files into C:\wamp\www\styles .inc.php files into C:\wamp\www\includes

Before I realized I had this problem, I tried to set up multiple virtual hosts using info from a number of websites (all of which said the same thing), but was unable to make that work at all, so I eliminated my changes and went with the WAMP default. I suspct the problems are related but can't solve either of them.

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • drwkqwa82140 2013-04-04 01:54

    I'm going out on a limb here.

    before I look into much more detail, I believe this is a simple oversight.

    just try:

    if (!isset($_SESSION['Authenticated']))
      header ("Location: home.php");
    } // this will basically re-direct to home.php if $_SESSION['Authenticated'] is not set

    But prior to performing the snippet posted above, just perform a:

    echo "http://$host$uri/$extra";

    with out the header(); just to see if this is where the problem is laying.

    解决 无用
    打赏 举报

相关推荐 更多相似问题