2015-09-04 86 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