2013-10-16 130 views
0

我似乎無法弄清楚爲什麼我的名字不會顯示出來。這是一個會議,我相信所有事情都是正確的。我必須需要第二套眼睛。此外,我似乎無法弄清楚如何連接鏈接1是正確的最後部分。它每次都會引發一個未定義的索引錯誤。無論如何,代碼是低於預先感謝。PHP會話名稱不會顯示和連接

<?php 
session_start(); 
//name 
if(isset($_SESSION['name'])){ 
    echo "Session Set"; 
} 
else if(!isset($_SESSION['name']) && isset($_COOKIE['name'])){ 
$_SESSION['name'] = $_COOKIE['name']; 
} 
else{ 
    $_SESSION['name'] = "Bill"; 
} 

//picUrl 
if(isset($_SESSION['picURL'])){ 
    echo "Session Set"; 
} 
else if(!isset($_SESSION['picURL']) && isset($_COOKIE['picURL'])){ 
$_SESSION['picURL'] = $_COOKIE['picURL']; 
} 
else{ 
    $_SESSION['picURL'] = "http://www.mugshots.org/misc/bill-gates.jpg"; 
} 

//bgColor 
if(isset($_SESSION['bgColor'])){ 
    echo "Session Set"; 
} 
else if(!isset($_SESSION['bgColor']) && isset($_COOKIE['bgColor'])){ 
$_SESSION['bgColor'] = $_COOKIE['bgColor']; 
} 
else{ 
    $_SESSION['bgColor'] = "black"; 
} 

//txtColor 
if(isset($_SESSION['txtColor'])){ 
    echo "Session Set"; 
} 
else if(!isset($_SESSION['txtColor']) && isset($_COOKIE['txtColor'])){ 
$_SESSION['txtColor'] = $_COOKIE['txtColor']; 
} 
else{ 
    $_SESSION['txtColor'] = "green"; 
} 

//hyperColor 
if(isset($_SESSION['hyperColor'])){ 
    echo "Session Set"; 
} 
else if(!isset($_SESSION['hyperColor']) && isset($_COOKIE['hyperColor'])){ 
$_SESSION['hyperColor'] = $_COOKIE['hyperColor']; 
} 
else{ 
    $_SESSION['hyperColor'] = "yellow"; 
} 

//vHyperColor 
if(isset($_SESSION['vHyperColor'])){ 
    echo "Session Set"; 
} 
else if(!isset($_SESSION['vHyperColor']) && isset($_COOKIE['vHyperColor'])){ 
$_SESSION['vHyperColor'] = $_COOKIE['vHyperColor']; 
} 
else{ 
    $_SESSION['vHyperColor'] = "red"; 
} 

//aHyperColor 
if(isset($_SESSION['aHyperColor'])){ 
    echo "Session Set"; 
} 
else if(!isset($_SESSION['aHyperColor']) && isset($_COOKIE['aHyperColor'])){ 
$_SESSION['aHyperColor'] = $_COOKIE['aHyperColor']; 
} 
else{ 
    $_SESSION['aHyperColor'] = "cyan"; 
} 

//link1 
if(isset($_SESSION['bgColor'])){ 
    echo "Session Set"; 
} 
else if(!isset($_SESSION['bgColor']) && isset($_COOKIE['bgColor'])){ 
$_SESSION['bgColor'] = $_COOKIE['bgColor']; 
} 
else{ 
    $_SESSION['link1'] = "http://en.wikipedia.org/wiki/Bill_Gates"; 
} 

//link2 
if(isset($_SESSION['link2'])){ 
    echo "Session Set"; 
} 
else if(!isset($_SESSION['link2']) && isset($_COOKIE['link2'])){ 
$_SESSION['link2'] = $_COOKIE['link2']; 
} 
else{ 
    $_SESSION['link2'] = "http://www.usdoj.gov/atr/cases/ms_index.htm"; 
} 

//link3 
if(isset($_SESSION['link3'])){ 
    echo "Session Set"; 
} 
else if(!isset($_SESSION['link3']) && isset($_COOKIE['link3'])){ 
$_SESSION['link3'] = $_COOKIE['link3']; 
} 
else{ 
    $_SESSION['link3'] = "http://www.microsoft.com"; 
} 

//link1Name 
if(isset($_SESSION['link1Name'])){ 
    echo "Session Set"; 
} 
else if(!isset($_SESSION['link1Name']) && isset($_COOKIE['link1Name'])){ 
$_SESSION['link1Name'] = $_COOKIE['link1Name']; 
} 
else{ 
    $_SESSION['link1Name'] = "Bill Gates Net Worth Page"; 
} 

//link2Name 
if(isset($_SESSION['link2Name'])){ 
    echo "Session Set"; 
} 
else if(!isset($_SESSION['link2Name']) && isset($_COOKIE['link2Name'])){ 
$_SESSION['link2Name'] = $_COOKIE['link2Name']; 
} 
else{ 
    $_SESSION['link2Name'] = "United States v. Microsoft"; 
} 

//link3Name 
if(isset($_SESSION['link3Name'])){ 
    echo "Session Set"; 
} 
else if(!isset($_SESSION['link3Name']) && isset($_COOKIE['link3Name'])){ 
$_SESSION['link3Name'] = $_COOKIE['link3Name']; 
} 
else{ 
    $_SESSION['link3Name'] = "Microsoft Home Page"; 
} 

?> 
<html> 

<head> 
<title>A05_SessionColors</title> 
</head> 

<body bgcolor="<?php echo $_SESSION['bgColor']?>" 
     text="<?php echo $_SESSION['txtColor']?>" 
     link="<?php echo $_SESSION['hyperColor']?>" 
     vlink="<?php echo $_SESSION['vHyperColor']?>" 
     alink="<?php echo $_SESSION['aHyperColor']?>"> 



<center> 
<h1><?php echo $_SESSION['name'] ?>'s Home Page</h1> 
<p><img border="0" src=<?php echo '"'.$_SESSION['picURL'].'"'?>/></p> 
<h2>My Three Favorite Links</h2> 
<ul> 
    <li> 
    <p style="line-height: 150%"><a href=<?php echo '"'.$_SESSION['link1'].'"'?>/><?php echo $_SESSION['link1Name']?></a></li> 
    <li> 
    <p style="line-height: 150%"><a href=<?php echo '"'.$_SESSION['link2'].'"'?>/><?php echo $_SESSION['link2Name']?></a></li> 
    <li> 
    <p style="line-height: 150%"><a href=<?php echo '"'.$_SESSION['link3'].'"'?>/><?php echo $_SESSION['link3Name']?></a>&nbsp;</li> 
</ul> 


</center> 

<h3 align="center">Click <a href="optionsForm.php">HERE</a> to change display options.</h3> 

<p>&nbsp;</p> 

</body> 

</html> 
+0

您的代碼極其重複。使用函數或循環的時間會縮短很多。 –

+0

是的,我知道。我剛剛開始使用PHP。你碰巧知道爲什麼我的問題正在發生。我其實想出了名字問題。鏈接3的連接使我陷入了陷阱 –

+0

我的歉意,我的意思是link1 –

回答

0

在你的重複代碼中,你錯過了一些數組鍵。我建議重寫這樣的:

<?php 
session_start(); 
$default_values = array(
    'name' => 'Bill', 
    'picURL' => "http://www.mugshots.org/misc/bill-gates.jpg", 
    'bgColor' => "black", 
    ... 
); 

foreach ($default_values as $key => $value) { 
    if(!isset($_SESSION[$key]) && isset($_COOKIE[$key])){ 
    $_SESSION[$key] = $_COOKIE[$key]; 
    } 
    else{ 
    $_SESSION[$key] = $value; 
    } 
} 

?> 
<html> 
... 

順便說一句,這似乎有點奇怪,你設置會話值cookie的值,當兩者都與目前的遊客依然存在。