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.

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

报告相同问题?

悬赏问题

  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集