Jump to content

Recommended Posts

I have a  login form on my site that, when the user logins in, it turns into a panel of controls they have.. it worked until i tried to put things into functions.. but now it displays the login form even if the person logins in...which its not supposed to! here is the file that has the function: (the function is called "navBar")

[code]<?php
function DisplayLinks(){
?>
<div align=left>
<a href="home">Home</a> | <a href="register">Register</a> | <a href="contact">Contact Us</a> | <a href="http://www.forums.zycogaming.xerodefect.com">Forums</a>
</div>
<?
}
?>

<?php
function show_errorPage(){
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<!-- DW6 -->
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>ZycoWorld</title>
<link rel="stylesheet" href="files/3col_rightNav.css" type="text/css">
</head>
<body link="#FF3333" vlink="#FF3333" alink="#000000">
<div id="masthead">
  <h1 id="siteName"><IMG SRC="files/zyLogo1.jpg"></h1>
  <div id="globalNav">
<? DisplayLinks(); ?></div>
  <br>
<hr align="left" width=55%>
<h2 id="pageName">Home</h2>
<!-- end masthead -->
<div id="content">
<hr>
  <div><span class="feature">
    <h3>You Must Be Logged In</h3>
    <p>
    You cannot view this page, you need to Login at the <A HREF="main.php">HOME</A> Page, or <A HREF=register>REGISTER</A> to be able to view.
  </div>
 
</div>
<!-- end content -->
  <? navBar(); ?>
  </div>
</div>
<!--end navBar div -->
<div id="siteInfo">
  <? siteInfo() ?>
  </div>
</div>
</body>
</html>
<?
}
?>

<?
function getVideos(){

$path = $_ENV['DOCUMENT_ROOT'];
$d = $path.'/uploads/uVideos/';
$dir = opendir($d);
while ($f = readdir($dir)) {
if (eregi("\.swf",$f))
$files[]=$f;
}
return $files;
}

function navBar(){
if($logged_in){
?>
<div id="navBar">
  <div id="search">
<form action="http://www.zycogaming.xerodefect.com/cgi-sys/entropysearch.cgi" target="searchwindow">
Zyco SiteSearch: <br><input name="query" value="" type="text">
<input name="user" value="zyco" type="hidden">
<input name="basehref" value="http://zycogaming.xerodefect.com" type="hidden">
<input name="template" value="default" type="hidden">
<input value="Search" type="submit">
</form>
  </div>
  <div id="sectionLinks">
    <h3>Your Controls</h3>
    <ul>
      <li><a href="home">Home</a></li>
      <li><a href="uVid">Video Upload</a></li>
      <li><a href="uPic">Picture Upload</a></li>
      <li><a href="editProfile">Edit zPage</a></li>
      <li><a href="/newlayout/<?php echo "$_SESSION[username]"; ?>.html ">View zPage</a></li>
      <li>Games</li>
    </ul>
  </div>
  <div class="relatedLinks">
    <h3>Affiliates</h3>
    <ul>
      <li><a href='http://flamelicker.com'><img src='http://flamelicker.com/newsite/affiliates/flamelicker.png'></a></li>
  <li><a href="http://www.xerodefect.com">XeroDefect Studios</a></li>
    </ul>
  </div>
  <div class="relatedLinks">
    <h3>Other</h3>
    <ul>
      <li><A HREF="http://www.zycogaming.xerodefect.com/brenden">Brenden's zPage</A></li>
  </li>
    </ul>
  </div>
</div>
<?
}else
?>
<div id="navBar">
  <div id="search">
<form action="http://www.zycogaming.xerodefect.com/cgi-sys/entropysearch.cgi" target="searchwindow">
Zyco SiteSearch: <br><input name="query" value="" type="text">
<input name="user" value="zyco" type="hidden">
<input name="basehref" value="http://zycogaming.xerodefect.com" type="hidden">
<input name="template" value="default" type="hidden">
<input value="Search" type="submit">
</form>
  </div>
  <div id="sectionLinks">
    <h3>Please Login</h3>
    <form action="" method="post">
      <table align="center" border="0" cellspacing="0" cellpadding="3">
<tr><td >Username:</td><td><input type="text" name="user" maxlength="30"></td></tr>
<tr><td >Password:</td><td><input type="password" name="pass" maxlength="30"></td></tr>
<tr><td colspan="2" align="left" ><input type="checkbox" name="remember">
<font size="2">Remember</td></tr>
<tr><td colspan="2" align="right"><input type="submit" name="sublogin" value="Login"></td></tr>
<tr><td  colspan="2" align="right"><a href="register">Register</a></td></tr>
</table>
    </form>
  </div>
<?
}
?>

<? 
  function siteInfo(){ 
  ?> 
  <div id="siteInfo"> 
    <p><a href='http://flamelicker.com'><img src='http://flamelicker.com/newsite/affiliates/flamelicker.png'></a> &copy;2006 ZycoWorld.com</p> 
    <div align=center> 
    <ul> 
    <A HREF=home>Home</A> | 
    <A HREF=/contact>Contact Us</A> 
    </ul> 
    </div>
  </div> 
  <? 
  } 
  ?>

<?
function editProfile(){
?>
<div align=center>*You are curently updating the profile for User:
<?php echo '<a href="' . $_SESSION['username'] . '.html">' . $_SESSION['username'] . '</a>'; ?>.
Click the name to see your profile.</div></b>
<p>
<hr>
<p>
<?php
$currentUser = "$_SESSION[username].html";
$loadcontent = "$currentUser";
    if($save_file) {
        $savecontent = stripslashes($savecontent);
        $fp = @fopen($loadcontent, "w");
        if ($fp) {
            fwrite($fp, $savecontent);
            fclose($fp);
print '<div align=center><font color=red><b>Profile has been updated! Click <a href='.$_SERVER[PHP_SELF].'>HERE</a>to refresh the Editor.</b></font></a></div>';

}
}
    $fp = @fopen($loadcontent, "r");
        $loadcontent = fread($fp, filesize($loadcontent));
$lines = explode("\n", $loadcontent);
$count = count($lines);
        $loadcontent = htmlspecialchars($loadcontent);
        fclose($fp);
for ($a = 1; $a < $count+1; $a++) {
$line .= "$a\n";
}
?>

<form method=post action="<?=$_SERVER[PHP_SELF]?>">
<table width="100%" valign="top" border="0" cellspacing="1" cellpadding="1">
  <tr>
    <td width="97%" align="center" valign="top"><textarea style="text-align: left; padding: 0px; overflow: auto; border: 3px groove; font-size: 12px" name="savecontent" cols="100" rows="25" wrap="OFF"><?=$loadcontent?></textarea></td>
  </tr>
</table>
<div align=center>
<input type="submit" name="save_file" value="-Save-">  |  <input type="submit" name="save_file" value="-Update Profile-">
</div>   
</form>
<p>
<hr>
<p>
<b>Want a profile like mine? Don't know HTML? No worries!! Copy the code below into the box above, edit it and BAM! You have a snazzy profile!! Just edit the CAPITAL PHRASES to match yourself.</b>
<br>
<textarea cols=100 rows=25>
<html>
<head>
<title>ENTER A TITLE TO YOUR PAGE HERE</title>
</head>
<body bgcolor=#F0E68C>
<div align=center>
<h1><u>YOUR USERNAME HERE</u></h1><br>
<A HREF=http://www.zycogaming.xerodefect.com/index.php>[HOME]</A>
<A HREF=http://www.zycogaming.xerodefect.com/register.php>[REGISTER]</A>
<A HREF=http://www.zycogaming.xerodefect.com/contactus.php>[CONTACT US]</A>
</div>
<p><p><p>
<hr>
<TABLE BORDER=0 CELLSPACING=1 CELLPADDING=0 BGCOLOR=FFCC66 ALIGN=CENTER>
<TR>
<TD ALIGN=left HEIGHT=25>
YOUR NAME HERE
</TD>
</TR>
<TR>
<TD ALIGN=left HEIGHT=100>
<img src ="YOUR IMAGE HERE" align ="left"><br>
Name: YOUR NAME HERE<br>
Age: YOUR AGE HERE<br>
Location: YOUR LOCATION HERE<br>
</TD>
</TR>
</TABLE>
<p>
<TABLE BORDER=0 CELLSPACING=1 CELLPADDING=0 BGCOLOR=FFCC66 ALIGN=CENTER WIDTH=350>
<TR>
<TD>
<i>About Me</i>:<br>
ENTER YOUR BIO OR FACTS HERE, ANYTHING YOU LIKE
</TD>
</TR>
</TABLE>
<br>
<TABLE BORDER=0 CELLSPACING=1 CELLPADDING=0 BGCOLOR=FFCC66 ALIGN=CENTER WIDTH=350>
<TR>
<TD>
<i>Music</i>:<br>
ENTER THE MUSIC YOU LISTEN TO, HERE
</TD>
</TR>
</TABLE>
</textarea>
<p>
<p>
<b>Want to add more sections?? Just modify this code and paste into your profile!!</b>
<br>
<textarea cols=100 rows=25>
<TABLE BORDER=0 CELLSPACING=1 CELLPADDING=0 BGCOLOR=FFCC66 ALIGN=CENTER WIDTH=350>
<TR>
<TD>
<i>ENTER HEADER HERE</i>:<br>
ENTER CONTENT HERE</TD>
</TR>
</TABLE>
</textarea>
<?
}
?>

<?
function headerLinks(){
if($logged_in){
?>
  <span class="relatedLinks">Hello,
  <strong><?php echo "$_SESSION[username]"; ?></strong>
.</span> | <A HREF=logout>Logout</A> | <a href="http://www.zycogaming.xerodefect.com/newlayout/<?php echo "$_SESSION[username]"; ?>.html">View zPage</a> | <a href="editProfile">Edit zPage</a> |<br>
<span class="feature">Your zPage URL: http://www.zycogaming.xerodefect.com/newlayout/<?php echo "$_SESSION[username]"; ?>.html<br><br>
<?
}
?>
</span>
<?
}
?>[/code]

is there any errors?? i dont see why its showing the login form and not the controls.....
Link to comment
https://forums.phpfreaks.com/topic/17341-php-problem-getting-really-mad-solved/
Share on other sites

Is the variable $logged_in being created out side of the functions. If it is you want to add [code=php:0]global $logged_in;[/code] in each function that uses the $logged_in variable. As functions have there own variable scope and so PHP cannot access variables out side of the function unless you declare them as global.
What he's stating is that a variable [i]outside[/i] the function() can't be accessed without actually [i]declaring it[/i] as a [b]global[/b] variable. Like this:

Won't work:

[code]<?php
$life = 42;
function meaningOfLife()
{
print "The meanin of life is being $life<br>";
}
meaningOfLife();
?>[/code]


When it prints it doesn't print the value of the $life variable. BUT, if you do this:

[code]<?php
$life = 42;
function meaningOfLife()
{
global $life;
print "The meanin of life is being $life<br>";
}
meaningOfLife();
?>[/code]

Then it will work and print the value. You may think that $life is a 'global' variable already. But not within a function it's not unless you declare it.

Hope this helps!
I kinda redid my entire layout in Dreamweaver and in stead of having a function that did this: "if user is logged in, show controls, else show login form" i added two seperate functions, one that shows the controls and one that shows the login form, and in my page i put...

[code]<?php
if($logged_in){
navBar();
}else{
loginBar();
}
?>[/code]

which is if the user is logged, show the "controls" function, otherwise shhow the loginform function.
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.