2015-09-04 90 views
0

这里是我的index.php中的代码广东话从其他PHP文件获取变量

include_once "connect.php"; 

$firstname = $_POST['firstname']; 
$lastname = $_POST['lastname']; 
$userName = $_POST['username']; 
$password = $_POST['pwd1']; 
$userName = stripslashes($userName); 
$password = stripslashes($password); 
$email = $_POST['email']; 

if (isset($_POST['Murad'])) { 
    if ($firstname =="" || $lastname =="" || $username =="" || $password =="" || $email ==""){ 
    include "all.php"; 
    echo ""; 
    exit(); 
    } 
if (strlen($userName) < 3 || strlen($userName) > 16) { 
     echo '<strong id="KIARTLAFF">3 - 16 characters please</strong>'; 
     exit(); 
} header("Location: main.php?/$firstname/$lastname/");} 
?> 

connect.php

<? 
$link = mysqli_connect('localhost', 'root', '123'); 
if (!$link) { 
    die('Could not connect: ' . mysqli_error()); 
} 
if(isset($_POST['Murad'])){ 
$db_selected = mysqli_select_db($link,'websiteusers'); 
$firstname = $_POST['firstname']; 
$lastname = $_POST['lastname']; 
$userName = $_POST['username']; 
$password = $_POST['pwd1']; 
$email = $_POST['email']; 

$sqlb = "INSERT INTO websiteusers (fullname,lastname,userName,pass,email) 
VALUES ('$firstname', '$lastname', '$userName', '$password', '$email')"; 
if(mysqli_query($link,$sqlb)){ 

}else { 
    echo mysqli_error($link); 
} 
mysqli_close($link); 
} 
?> 

而且main.php

<?php 
include_once "connect.php"; 
?> 
<title ><?php echo $firstname." ".$lastname;?></title> 

我想从index.php到main.php的变量。 我找不到,可以帮助我在interenet有许多包括我尝试过任何方式,但他们没有工作

+0

邮他们,形式发布它们,并使用'$ _SESSION' – Shehary

+1

包括你的connect.php,而不是在你的index.php。 php文件 – Unex

+0

你想重定向到main.php吗? – Ahmad

回答

2

一对夫妇的问题在这里:

  1. 在你if条件你有if (..$username == ""..)然而, $username未声明的,而不是你有$userName,从而header()从未达到

  2. 您使用此:header("Location: main.php?/$firstname/$lastname/");这将正确地重定向到URL,但不会做任何事情为y ou可能是想着$_GET,但是这个url不是一个合适的get url。你会需要像main.php?firstname=$firstname?lastname=$lastname。这在main.php回报,你将需要检索这些获取的参数,如$fname = $_GET['firstname']


UPDATE:

如果你真的想弄死整个重定向的想法,那么你需要稍微改变你的代码。

Assumming这是流量:

  1. 用户提交从someother.php
  2. 形式的形式发送POST请求main.php

然后,你需要做到以下几点:

  1. 删除该行所在的行header()index.php
  2. main.php应该是这样的:

    <?php 
    include "index.php"; 
    ?> 
    <html> 
        <head> 
         <title><?php echo $firstname; ?></title> 
        </head> 
        <body> 
         <div><?php echo $firstname; ?></div> 
        </body> 
    </html> 
    
+0

所以我可以得到第一个名称和用户名从URL? – qwaaz

+0

是的,你可以只要它的URL格式正确 – CodeGodie

+0

这个网址是对的? http://localhost/murad/centil/o/main.php?firstname = a?lastname = aaaaaaaaa – qwaaz