Jump to content

[SOLVED] Why does $username work for one bit and not another


Recommended Posts

I have session_start(); etc.

<?php
if($_SESSION['s_logged_n'] == 'true'){
$username = $_SESSION['s_username'];
echo('<strong>'.ucwords($username).' - <a href="member.php?user=$username">My Account</a> - <a href="logout.php">Logout</a></strong>');
}
?>

 

.ucwords($username) works fine when testing it. Though for some reason user=$username doesn't. It stays default, so in the browser it comes up as "www.mysite.com/member.php?user=$username" doesn't bring up the logged in users username.

 

Hope you can help.

Incase for further reference, here is the full page code including HTML. Might not be needed though depends on the problem.

 

<?php session_start(); 

include 'config.php';

//Checks if there is a login cookie
if(isset($_COOKIE['ID_username']))
//if there is, it logs you in and directes you to the members page
{ 
$username = $_COOKIE['ID_username']; 
$pass = $_COOKIE['Key_password'];
$activated = $_COOKIE['Key_activated'];
$check = mysql_query("SELECT * FROM Users WHERE Username = '$username'")or die(mysql_error());
while($info = mysql_fetch_array( $check )) 
{
if ($pass != $info['Password']) 
{
	}
	else
	{
		if ($activated != $info['Activated'] > 0)
		{
		    }
			else
			{
		$_SESSION['s_logged_n'] = 'true';
		$_SESSION['s_username'] = $username;
		$_SESSION['s_name'] = $row['Name'];
			}
		}
	}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="Content-Language" content="en-UK" >
<meta name="description" content="Developers Community is a Community for developers to discuss and provide support for development. We have forums that allow users to participate in polls, giving the support to other users, or you may perhaps share your knowledge so that others can learn from you. Additionally, we have tutorials which cover a wide aspect of all scripts, from HTML to PHP to C++" >
<meta name="keywords" content="script help, script forums, script tutorials, script tutorial, script snippets, script, help, resources, snippets, tutorials, web development, programming, development, community, developers community, php, c++, c#, MySQL, database, databases, VB, Java, Javascript, support" >
<title>Developers Community - Index</title>
<link href="styles/twoColLiqRtHdr.css" rel="stylesheet" type="text/css" />
<link rel="shortcut icon" href="images/favicon.ico">
<link rel="icon" href="images/favicon.ico">
<!--[if IE]>
<style type="text/css"> 
/* place css fixes for all versions of IE in this conditional comment */
.twoColLiqRtHdr #sidebar1 { padding-top: 30px; }
.twoColLiqRtHdr #mainContent { zoom: 1; padding-top: 15px; }
/* the above proprietary zoom property gives IE the hasLayout it needs to avoid several bugs */
</style>
<![endif]-->
</head>

<body class="twoColLiqRtHdr">
<div id="container">
  <div id="header"> 
  <div class="twoColLiqRtHdr" id="logreg">
  <?php 
if($_SESSION['s_logged_n'] == 'true'){
$username = $_SESSION['s_username'];
echo('<strong>'.ucwords($username).' - <a href="member.php?user=$username">My Account</a> - <a href="logout.php">Logout</a></strong>');
} else { ?>
<strong><a href="login.php">login</a> // <a href="register.php">register</a></strong>
<?php } ?>
</div>
  </div>
  <div>		
  <ul id="navigator">
		<li class="active"><a href="/">Home</a></li>
		<li><a href="tutorials.php">Tutorials</a></li>
		<li><a href="jobvac.php">Job Vacancies</a></li>
            <li><a href="contactus.php">Contact Us</a></li>
		<li><a href="forums">Forums</a></li>            
<div id="time" style="font-size:12px";>
<script type="text/javascript">
var d=new Date()
var weekday=new Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday")
var monthname=new Array("January","February","March","April","May","June","July","August","September","October","November","December")
document.write(weekday[d.getDay()] + " ")
document.write(d.getDate() + " ")
document.write(monthname[d.getMonth()])
</script>
</div>
    </ul>
  <div class="twoColLiqRtHdr" id="topbar"> </div>
  </div>
  <div id="sidebar">
    <div id="updates">
      <h3 style="margin:0px;">Recent News</h3><br />
      <?php include("Updates/updatediv.php"); ?>
      <br />
</div>
<div class="twoColLiqRtHdr" id="advertisement">
  <h2><br />
    <br />
    <br />
    Want your advert here? Contact us.<br />
    <br />
    <br />
    <br />
  </h2>
</div>
  </div>
  <div id="mainContent">

    <div class="twoColLiqRtHdr" id="introduction">
      <h1>Homepage </h1>
      <p>Welcome to Developers Community. This is our homepage which displays all kinds of information. It explains a bit about us, it has updates along with every other page has update sections. It will also display the latest tutorials that has been submitted to the website. More information might be put here if it was nessecary.</p>
    </div>
<h2>What is Developers Community?</h2>
    <p>We are a community for developers. This community is about discussing recent development around  the "computer". There is a forum for you to post in, this has a whole range of things, from discussing the latest Operating System, to recieving help for your own website or software. This website is mostly about helping out those who cannot find the answer to there errors in there scripts elsewhere, though does involve other things to do. In this community, as already explained you can discuss development and recieve help on your errors. You can also post your own tutorials to be on this website for people all over the worldwide web to use, and credits will go to you. We aim for Developers Community to be the most helpful forums ever. With your help, we can achieve this!
</p>
    <p>We do not tolerate abuse. If for some reason you believe you or your son/daughter is being abused here on Developers Community. Please contact us with details on the abuse and we'll get back to you as soon as possible.<br />
    In some cases, people miss-interpret what someone is saying; as abuse. If this is the case for you, we will help you to understand how it is not abuse.</p>
    <p>If there is anything you'd like to know. Feel free to contact us and we'll reply as soon as we can!<br />
    <div class="twoColLiqRtHdr" id="tutorials">
      <div class="twoColLiqRtHdr" id="tutorialstitle">
      <h2 style="margin:0px; text-align: center;">Latest Tutorials</h2></div>
      <div class="twoColLiqRtHdr" id="tutorialsinfo">
        <p> </p>
        <p>This function isn't avaliable right now.</p>
      </div>
</div>
    <div class="twoColLiqRtHdr" id="member">
      <div class="twoColLiqRtHdr" id="membertitle">
      <h2 style="margin:0px; text-align: center;">Member Of The Week</h2></div>
      <div class="twoColLiqRtHdr" id="memberinfo">
        <p> </p>
        <p>This function isn't avaliable right now.</p>
      </div>
   </div>
    <p><br /> 
    <br /> <br /> <br /> <br /> <br /> <br /> <br /></p>
    <p><br /></p>
  </p></div>
  <br class="clearfloat" />
  <div id="footer">
    <p>Developers-Community © 2009 - <a href="contactus.php">Contact</a></p>
  <!-- end #footer --></div>
<!-- end #container --> </div>
</body>
</html>

 

It's a bit messy.

echo('<strong>'.ucwords($username).' - <a href="member.php?user=' . $username . '">My Account</a> - <a href="logout.php">Logout</a></strong>');

 

Because you are in single quotes, $ are taken literally. You have to concatenate the variable into the string for it to show.

Ok thank you.

 <?php header("location: member.php?user=$username"); ?> 

Howcome that rule doesn't go for header locations too?

 

It does, try this:

 <?php header('location: member.php?user=$username'); ?> 

 

It will print $username.

 

A variable inside double quotes ( " ) is interepted. But inside a single quote ( ' ) it is taken literally, this not interepted/parsed. If that does not make sense, please let me know and I will elaborate.

It is good practise to simply use single quotes with any php statements as html uses doubble quotes usually and then to  escape any sinlge quotes in a html statment instead of the whole line by using doubble quotes.

 

Using single quotes however does mean that you have to concatinate, but that allows you to see your variables more clearley as they will get highlighted with any good php html editor.

 

And the example you provided above does use the same rule but you are using doubble quotes, if you change it to single quotes it will not work.

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.