douke1942 2013-04-04 01:47
浏览 99
已采纳

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");
    exit;
    }

Original version, also not working, was:

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

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
127.0.0.1           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
#####NameVirtualHost 127.0.0.1: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>
    ServerAdmin webmaster@dummy-host.example.com
    DocumentRoot "c:/apache2/docs/dummy-host.example.com"
    ServerName dummy-host.example.com
    ServerAlias www.dummy-host.example.com
    ErrorLog "logs/dummy-host.example.com-error.log"
    CustomLog "logs/dummy-host.example.com-access.log" common
</VirtualHost>

<VirtualHost *:80>
    ServerAdmin webmaster@dummy-host2.example.com
    DocumentRoot "c:/apache2/docs/dummy-host2.example.com"
    ServerName dummy-host2.example.com
    ErrorLog "logs/dummy-host2.example.com-error.log"
    CustomLog "logs/dummy-host2.example.com-access.log" common
</VirtualHost>

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

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");
      exit;
    } // 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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 关于#python#的问题:自动化测试