PHP displaying unique data - php

currently I have a website with a basic login, I was just wondering how I would display a the name,skill and description of the unique user who is logged in.This is what I have done so far.I can only find articles on how to display data into table.This is the updated code:
<?php
include('session.php');
require 'config.php';
$sql = "SELECT * FROM profile";
$result = $conn->query($sql);
//echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "<br>";
?>
<html>
<head>
<link rel="stylesheet" type="text/css" href="profile.css">
</head>
<body>
<ul>
<li>Home</li>
<li>PostJob</li>
<li>Find Job</li>
<li>How It Works</li>
<li>Notifications</li>
<li>Message</li>
<li>profile</li>
</ul>
<h1>Welcome To Bid4MyJob</h1>
<li>edit profile</li>
<div id="ProfilePage">
<div id="LeftCol">
<div id="Photo"></div>
<div id="ProfileOptions">
a
</div>
</div>
<div id="Info">
<p>
<strong>Name:<?php echo $row["name"]?></strong>
<!--<span>James</span>-->
</p>
<p>
<strong>Skill:<?php echo $row["skill"]?><</strong>
<!--span>James</span>-->
</p>
<!-- <p>
<strong>review:<?php /*echo $row["review"]*/?><</strong>
<span>james</span>
</p> -->
<p>
<strong>Description:<?php echo $row["description"]?><</strong>
<span>James</span>
</p>
<!--<p>
<strong>Name:</strong>
<span>james</span>
</p>-->
</div>
<!-- Needed because other elements inside ProfilePage have floats
<div style="clear:both"></div>-->
</div>
</body>
</html>

when the user successfully loggedin create a cookie and store their username or email whatever you used in that cookie. You have to do this stuff in your login.php file where you checks for username and password.
if(login success)
{
setCookie("username",value of username that you got from
user,'time()+3600','/');
echo "login successful";
}
After that in Profile you have write code like this;
<?php
$name=$_COOKIE['username'];
$sql=$conn->prepare("SELECT * from profile where username=?");
$stmt->bind_param('s', $name); // 's' specifies the variable type =>'string'
$stmt->execute();
$result = $stmt->get_result();
while ($row = $result->fetch_assoc()) {
// do something with $row
}
?>
After that you can display the inforamtion in your div like $row['username'] etc.

if $row[] come from a sql request, just add in your request something like :
$user = (...) your current user logged
(...)
select (...) where user = $user

You can use $_SESSION to identify people after login and store information about the person. A session variable saves data of a specific user across multiple pages.
A session id is stored in the user browser and that is used to identify the $_SESSION data of a person.
In your program you can easily do something like this:
<?php
session_start(); //before anything else
include('session.php');
require 'config.php';
$sql = "SELECT * FROM profile";
$result = $conn->query($sql);
//assuming $row contains the information from
$_SESSION["user"] = $row;
?>
<html>
<head>
<link rel="stylesheet" type="text/css" href="profile.css">
</head>
....
....
Now on another page, you could easily:
<?php
session_start();
if(isset($_SESSION["user"]) {
//logged in
$name = $_SESSION["user"]["name"];
} else {
//not logged in
}
?>
<html>
....
....

Related

Unknown issue with $_SERVER["REQUEST_METHOD"] returning false when it should be true

Hey there stackoverflow users, i have come upon a very confusing problem that I cant seem to move past. I am creating a forum type web page and am currently working on the comments section. I have a form that uses the post method to send your comment as well as a hidden input to store the threads ID. I will post the entire php file below just to make sure nothing is left out.
<?php
session_start();
parse_str($_SERVER['QUERY_STRING'], $link);
$threadID = $link['ID'];
require("config.php");
$connection = mysqli_connect($host, $user, $password, $database);
$error = mysqli_connect_error();
if($error != null) {
$output = "<p>Unable to connect to database!</p>";
exit($output);
} else {
//Get Thread Data
$query = "SELECT username, title, content FROM threads, users WHERE threads.ID = $threadID AND users.ID = threads.makerID;";
$results = mysqli_query($connection, $query);
$row = mysqli_fetch_assoc($results);
//Get Comment Data
$query = "SELECT username, comment FROM comments, users WHERE threadID = $threadID AND users.ID = comments.makerID;";
$results = mysqli_query($connection, $query);
$row = mysqli_fetch_assoc($results);
}
?>
<!DOCTYPE html>
<html>
<head lang="en">
<title>BodyweightMate</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="../css/styling.css"/>
</head>
<body>
<!--Top masthead-->
<header class="masthead" id="top">
<h1 class="masthead-title"> Welcome To BodyweightMate </h1>
</header>
<!--Navigation bar-->
<nav class="navbar">
<table class="navbar-table">
<tr>
<!--Logo-->
<td>
<a class="navbar-brand" href="main.php">
<img src="../images/logo.jpg" alt="BodyweightMate" height="30" width="30">
</a>
</td>
<!--Login/Profile-->
<?php if(isset($_SESSION['login_user'])) {
echo"<td><a class=\"navbar-profile\" href=\"profile.php\"> Profile </a></td>";
echo"<td><a class=\"navbar-logout\" href=\"logout.php\"> Logout </a></td>";
} else {
echo"<td><a class=\"navbar-login\" href=\"login.php\"> Login </a></td>";
}?>
</tr>
</table>
</nav>
<!--Main portion-->
<section class="content-section">
<article>
<h3><?php echo $row['username']. ": " .$row['title']; ?></h3>
<p><?php echo $row['content']; ?></p>
<br>
<h3>Comments</h3>
<p>Some annoying user: Gr8 B8 M8</p>
<p>Annoying users friend: I R8 8/8</p>
</article>
<div>
<!--If logged in, ability to comment-->
<?php if(isset($_SESSION['login_user'])): ?>
<form role="comment-form" method="POST" action="processcomment.php" id="mainForm">
<input type="hidden" value="$threadID" name="threadID">
<div class="form-group">
<label for="comment">Comment </label> <br>
<textarea class="comment-text" name="comment" rows="2" maxlength="255"></textarea>
</div> <br>
<input type="Submit" class="btn-newcomment" value="Submit Comment" name="submit">
</form>
<?php endif ?>
</div>
</section>
<!--Right portion-->
<aside class="content-aside">
<div>
<!--If logged in, be able to create a thread-->
<?php
if(isset($_SESSION['login_user'])) {
echo"<form method=\"post\" action=\"makethread.php\">";
echo"<input type=\"submit\" class=\"btn-newthread\" value=\"Create New Thread\" name=\"submit\">";
echo"</form>";
}
?>
</div>
<!--Info-->
<div>
<p> GOING TO NEED A SEARCH FUNCTION HERE
This is the cool little aside section. It will always be there to provide you with some very nice little details, helpful links, maybe a list of moderators? who knows! The uses are endless when you have a beautiful little aside like this one! Here are a few very useful bodyweight fitness links to get us started :D </p>
</div>
<br>
<div>
<ul class="content-aside-links">
<li>
Reddit's Bodyweightfitness Forum
</li>
<li>
Reddit's Bodyweightfitness RR
</li>
<li>
Antranik's Bodyweightfitness Routine
</li>
</ul>
</div>
<div></div>
</aside>
<!--Footer -->
<footer class="footer">
<div>
<p> Use of this site constitutes acceptance of our User Agreement © 2017 BodyweightMate inc. All rights reserved. </p>
</div>
</footer>
</body>
</html>
The error is occurring under the main portion where i check if a user is logged in, and if they are add a short form consisting of a message, a text area, and a submit button. This form sends the information to the following php file.
<?php
session_start();
if(!isset($_SESSION['login_user'])) { header("location: main.php"); }
?>
<!DOCTYPE html>
<html>
<body>
<?php
require("config.php");
$connection = mysqli_connect($host, $user, $password, $database);
$error = mysqli_connect_error();
if($error != null) {
$output = "<p>Unable to connect to database!</p>";
exit($output);
} else {
//Validation
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$comment = $_POST['comment'];
$threadID = $_POST['threadID'];
$user = $_SESSION['login_user'];
} else {
//Redirect back to register
echo"<p>Form must use post or input was bypassed.</p>";
echo" Return to home page. ";
mysqli_close($connection);
exit();
}
There is no issue with connecting to the database, and I don't believe the remainder of the code is necessary to help me with this error since that one if statement of checking if the form is using post is failing and the else statement is always called. Why is this? i have rewritten the form multiple times ensuring that its properly structured and using post yet it fails every time!

Retrieve Single User Data Using Session After He Logged In [duplicate]

This question already has answers here:
How can I get an unknown username given an ID?
(2 answers)
Closed 1 year ago.
i am new to this field and this is the first time i am working with session, the question may seem very basic but would appreciate if someone could help me. Currently I have made a login and logout page using session and wish to display data of the particular user who has logged in. The user is redirected to retailer_login.php after they sign in, apart from login form there are 4 pages for the entire login and logout process.
retailer_login.php, retailer_session.php, retailer_profile.php, retailer_logout.php
Every page is working fine however i am able to display only single data column of the user from database but i wish to display the entire information that is stored about that specific user.
DATABASE
Id name email password country city state occupation
1 sam sam#gmail.com sam XYZ ZBC QWE student
retailer_login page
<?php
session_start(); // Starting Session
if (isset($_POST['submit'])) {
try {
if (empty($_POST['email']) || empty($_POST['password'])) {
throw new Exception("email or Password is invalid");
} else {
// Define $email and $password
$email = $_POST['email'];
$password = $_POST['password'];
// To protect MySQL injection for Security purpose
$email = stripslashes($email);
$password = stripslashes($password);
$mail = mysql_real_escape_string($email);
$password = mysql_real_escape_string($password);
//Etablishing Connection with Server by passing server_name, user_id and password as a parameter
$connection = mysql_connect("abc.com", "abc", "abc");
// Selecting Database
$db= mysql_select_db("abc", $connection);
// SQL query to fetch information of registerd users and finds user match.
$query = mysql_query("select * from retailerregister where password='$password' AND email='$email'", $connection);
$rows = mysql_num_rows($query);
if ($rows != 1)
throw new Exception("email or Password is invalid");
$_SESSION['login_user'] = $email; // Initializing Session
header("location: retailer_profile.php"); // Redirecting To Other Page
mysql_close($connection); // Closing Connection
}
}
catch (Exception $e) {
$_SESSION['login_error'] = $e->getMessage();
header("Location: index.html");
}
}
?>
retailer_profile page
<?php
include('retailer_session.php');
?>
<!DOCTYPE>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Welcome to your homepage</title>
<meta name="viewport" content="width=device-width", initial-scale=1.0">
<link href="css/bootstrap.min.css" rel="stylesheet" />
<link href="css/styles.css" rel="stylesheet" />
<link href="css/carousel.css" rel="stylesheet">
<link href="css/style.css" rel="stylesheet">
</head>
<body>
<div id="profile">
<div class="navbar navbar-inverse navbar-static-top">
<div class="container">
<id="welcome">Welcome : <i><?php echo $login_session; ?></i>
<button class = "navbar-toggle" data-toggle = "collapse" data-target = ".navHeaderCollapse">
<span class = "icon-bar"> </span>
<span class = "icon-bar"> </span>
<span class = "icon-bar"> </span>
</button>
<div class="collapse navbar-collapse navHeaderCollapse">
<ul class = "nav navbar-nav navbar-right">
<li class ="active"> Home</li>
<li> Profile</li>
<li class="dropdown">
Property <b class ="caret"></b>
<ul class="dropdown-menu">
<li> Add property </li>
<li> View property </li>
</ul>
</li>
<li> <id="logout">Log Out</li>
</ul>
</div>
</div>
</div>
</div>
<div name="container">
</div>
<script src = "js/jquery-1.11.1.js"> </script>
<script src = "js/bootstrap.js"> </script>
</body>
</html>
retailer_logout page
<?php
session_start();
if(session_destroy()) // Destroying All Sessions
{
header("Location: index.html"); // Redirecting To Home Page
}
?>
retailer_session page
<?php
// Establishing Connection with Server by passing server_name, user_id and password as a parameter
$connection = mysql_connect("abc.com", "abc", "abc");
// Selecting Database
$db = mysql_select_db("abc", $connection);
session_start();// Starting Session
// Storing Session
$user_check=$_SESSION['login_user'];
// SQL Query To Fetch Complete Information Of User
$ses_sql=mysql_query("select * from retailerregister where email='$user_check'", $connection);
$row = mysql_fetch_assoc($ses_sql);
$login_session =$row['email'];
if(!isset($login_session)){
mysql_close($connection); // Closing Connection
header('Location: index.html'); // Redirecting To Home Page
}
?>
right now i am only able to use $login_session in order to display email on profile page. Can anyone please tell my how to display other data of the logged in user on the retailer_profile page through session
Just create another variables about current logged in user:
$row = mysql_fetch_assoc($ses_sql);
$login_session =$row['email'];
// another user data
$user_name = $row['name'];
$user_country = $row['country'];
$user_city = $row['city'];
$user_state = $row['state'];
$user_occupation = $row['occupation'];
Or you can just use one variable which shouldn't be overwritten:
$user_data = $row;
And then somewhere in script:
echo $user_data['city']; // etc...

PHP use a variable from a different file

I have a login page which records the username that the user enters and adds it to a variable of $uname. However when the page after the login page loads, I cannot echo the $uname. For example, when i type
Welcome <?php echo $uname; ?>
it does not insert the username.
below is a copy of my login-validation code. but I am not sure if the $_SESSION variable is working correctly, or how to reference it in my profile.php file.
<?php
session_start();
$_SESSION['uname'] = $uname;
// Grab User submitted information
$uname = $_POST["uname"];
$pass = $_POST["pass"];
// Connect to the database
$con = mysql_connect("mysql.*********.co.uk","******","************");
// Make sure we connected succesfully
if(! $con)
{
die('Connection Failed'.mysql_error());
}
// Select the database to use
mysql_select_db("onedirectionaffection_members",$con);
$result = mysql_query("SELECT uname, pass FROM users WHERE uname = $uname");
$row = mysql_fetch_array($result);
if($row["uname"]==$uname && $row["pass"]==$pass)
header("Location: ../../profile/profile.php");
else
echo"Sorry, your credentials are not valid, Please try again.";
?>
If anyone could help I would be hugely thankful. Also, I am an absolute beginner at all of this so if you need anymore details I'll try my best to answer.
profile.php
<?php
session_start();
echo $_SESSION['uname'];
?>
<html>
<head>
<title>1D Affection</title>
<link rel="stylesheet" Type="text/css" href="../css/stylesheet.css" />
<link rel="stylesheet" Type="text/css" href="../css/font.css" />
<link rel="stylesheet" Type="text/css" href="../css/profile.css" />
</head>
<body bgcolor="white">
<div id="wrapperhead">
<div id="headcont">
<div class="logo">
<img src="../images/1DA logo ripped.png" height="150px">
</div>
<div class="subheading">
<img src="../images/1d subheading.png" height="150px">
</div>
</div>
</div> <!--END OF HEADER-->
<div id="nav">
<div class="navigation">
<ul>
<li><a class="nav" href="../index.html">Home</a></li>
<li><a class="nav" href="#">News</a></li>
<li><a class="nav" href="#">Fan-fiction</a></li>
<li><a class="nav" href="#">Gallery</a></li>
<li><a class="nav" href="#">Testimonials</a></li>
<li><a class="nav" href="http://www.onedirectionstore.com/" target="_blank">Store</a></li>
</ul>
</div> <!-- END OF MENU-->
<!-- END OF NAVIGATION-->
</div>
<div id="wrappercontent">
<div class="content">
<div class="maincont">
<div class="profcust">
<div class="profpic">
</div>
<div class="profinfo">
</div>
</div>
<div class="username">
Welcome <?php session_start(); echo $uname; ?>
</div>
<div class="story">
</div>
</div>
<div class="sidenav">
Coming Soon
</div>
</div><!--end of content-->
</div>
</body>
</html>
Seems like you haven't added session_start(); on top of your profile.php page.
Try like this
//profile.php
<?php
session_start();
echo $_SESSION['uname'];
This is probably a good part of the issue.
$_SESSION['uname'] = $uname;
$uname = $_POST["uname"];
Your setting your session's uname to blank on every load of that page. Put $_SESSION['uname'] = $uname; at the end of the code when it's validated.
1) You need to add a value to $uname first, then assign its value to $_SESSION element, so it's better be like this:
$uname = $_POST['uname'];
$_SESSION['uname'] = $uname;
or even like this:
$_SESSION['uname'] = $_POST['uname'];
2) As already mentioned, At profile.php you should also have session_start();
3) Make a clean exit like this:
header("Location: ../../profile/profile.php");
exit();
My bet is that it should be working fine after.
Some how, this is now working. From what I can figure out, the solution was to call in the $_SESSION variable, and then wrap that inside another variable. so
<?php
session_start();
$uname = $_SESSION['uname'];
?>
Thanks for all the help :D
session_start(); needs to be inside all pages using sessions.
I tested the following:
<?php
session_start(); // page_2.php
echo "Welcome " . $_SESSION['uname'];
?>
In conjunction with my test page: page_a.php
<?php
session_start();
$uname = "FRED";
$_SESSION['uname'] = $uname;
?>
CLICK
Echo'ed Welcome FRED on page 2.
I also noticed you have another instance of session_start(); in your page profile.php, remove it because you will be starting a new session while overwriting your first.
<div class="username">
Welcome <?php session_start(); echo $uname; ?>
</div>
Therefore you should be using:
$uname = $_SESSION['uname'];
in conjunction with:
<div class="username">
<?php echo "Welcome " . $_SESSION['uname']; ?>
</div>
As berkes stated in this comment you have a security issue:
$uname = $_POST["uname"];
$pass = $_POST["pass"];
Change it to:
$uname = mysql_real_escape_string($_POST['uname']);
$pass = mysql_real_escape_string($_POST['pass']);
MySQL_ functions are deprecated, therefore using MySQLi_ with prepared statements is highly suggested or PDO.
Do read the following articles:
How can I prevent SQL injection in PHP?
On owasp.org

Let users visit other users (profiles) in my own written social community?

The thing I'm asking for is how the users can visit other users profile.
The past week I have been scripting a social network I'm doing, and I'm kind of stuck right now.
I don't know where to start and read or anything.
So I'm asking you guys to kindly help me =)
Im thinking of the url to be like user.php?id=123 looking and get user you are visiting to show up instead of "you" wich is at user.php!
Live demo: http://social.swipper.org
EDIT #2:
Here's the current code i got inside user.php :
<?php
session_start();
if($_SESSION['username']) {
include "config.php";
$username = $_SESSION['username'];
$fetcher = mysql_query("SELECT * FROM userinfo WHERE username='$username'");
while ($data = mysql_fetch_assoc($fetcher)){
$firstname = $data['firstname'];
$lastname = $data['lastname'];
$gender = $data['gender'];
}
// get user's profile image
if (file_exists("users/$username/profile.jpg")) {
$userPhoto = "<img class='pic' src='users/$username/profile.jpg' width='90%' height='30%' />";
}
elseif (!file_exists("users/$username/profile.jpg")) {
$userPhoto = "<img class='pic' src='http://www.uavmedia.com/portal/images/no-user.jpg' width='90%' height='30%' />";
}
// the user's profile image is fetched
// henter profil text
$file = "users/$username/bio.txt";
$contents = file($file);
$profile_text = implode($contents);
// ferdig å hente profil text, vil nå echo ut profil siden.
// henter profil stilsett
$file2 = "users/$username/style.css";
$contents2 = file($file2);
$profile_style = implode($contents2);
// ferdig å hente profil stilsett.
echo
"
<!doctype html>
<html lang='en'>
<head>
<title>Social FW</title>
<meta name='Description' content='A Social network for everyone. Come join us!' />
<meta name='Keywords' content='Social, Network, Framewerk, Framework, FW, Open-Source, Free' />
<link rel='stylesheet' type='text/css' href='user_files/style.css' media='screen' />
<link rel='stylesheet' type='text/css' href='SFW_files/style2.css' media='screen' />
<style type='text/css'>
$profile_style
</style>
<link rel='icon' href='SFW_files/favicon.ico' media='screen' />
<script type='text/javascript' src='http://code.jquery.com/jquery-latest.js'></script>
<script type='text/javascript' src='SFW_files/scripts/login.js'></script>
</head>
<body>
<div id='top'>
<h1>Swipper.org</h1>
</div>
<div id='side-menu'>
<ul>
<li><a href='user.php'><b>".$username."</b></a></li><br/>
<li><a href=''>Inbox</a></li>
<li><a href=''>Guestbook</a></li>
<li><a href=''>Friends</a></li>
<li><a href=''>Pictures</a></li><br />
<div id='showOptions'><li><a href='#'>Edit Profile</a></li></div>
<div id='editOptions' style='display:none;'>
<pre class='user_info'><b>></b><a href='changeText.php'>Edit Profile Text</a>
<b>></b><a href='changeCss.php'>Edit Stylesheet(CSS)</a>
<b>></b><a href='changeProfilePic.php'>Change Profile Image</a></pre>
</div><br />
<a href='logout.php'><b>Logout</b></a>
</ul>
</div>
<div id='side-left'>
<!-- START USER PIC --!>
<center>
$userPhoto<br />
</center>
<!-- END USER PIC --!>
<!-- START USER INFO --!>
<br />
<h3><pre class='user_info'>User Info</pre></h3>
<pre class='user_info'>Name: ".$firstname." ".$lastname."<br />Sex : $gender</pre>
<!-- END USER INFO --!>
</div>
<div id='box_center'>
<h2 style='font-size:30px;' align='center'>$username</h2>
<hr />
<br />
$profile_text
</div>
<div id='footer'>
<p align='center'>Swipper.org © All rights reserved. 2010-2012</p>
</div>
</body>
</html>
";
}
else {
die("You need to login first or register. You can register/login <a href='index.php'>here</a>!");
}
?>
and when the url is like: user.php?id=3 i want to get the user with userid 3 to show up, and get user #3's information instead of the "user" who wants to visit other people.
Are you listing all the users some where? if you are the href needs to be something like
<?php
$sql = mysql_query("SLECET * FROM userinfo");
while ($row = mysql_fetch_assoc($sql)){
echo "<a href='users.php?id=" . $row['id'] . "'>"
}
then in users.php
<?php
if(isset($_REQUEST['id'])) {
if(is_numeric($_REQUEST['id'])){
$uid = $_REQUEST['id'];
$sql = mysql_query("SELECT * FROM userinfo WHERE id='" . $uid . "' LIMIT 1");
}
} else {
$sql = mysql_query("SELECT * FROM userinfo WHERE username='" . $_SESSION['username'] . "' LIMIT 1");
}
then later your html can use that with a fetch_assoc($sql)
<table>
<tr>
<td><?php echo $row['username'] ?>'s Profile</td>
</tr>
</table>
simple example. but i think you get the picture, message me on FB for more on this Stian
This should be your answer:
if (isset($_GET['username']){
$username = mysql_escape_string($_GET['username']);
}else{
$username = $_SESSION['username'];
}
$fetcher = mysql_query("SELECT * FROM userinfo WHERE username='$username'");
if (mysql_num_rows() == 0){
$username = '';
echo 'Username not found'; die();
}
Make your query like
$userID = $_GET['id'];
$fetcher = mysql_query("SELECT * FROM userinfo WHERE userID='$userID'");
and you can accordingly show data for any user with the valid user ID as given in url.
if profile can be viewed without login too then there is no need of session and if its compulsory only check if isset or not. if you are passing username in url as
user.php?user=test1
then your query will be like
$username = $_GET['user'];
$fetcher = mysql_query("SELECT * FROM userinfo WHERE username='$username'");
Hope now its more clear to u.

Making Variables Global in PHP?

Need to make $courseInfo and $row global so that they can be used for printing the row details in the header DIV.
Don't have a clue how to do this. Any help would be great.
<?php
// Get Course ID From Link
$ID = mysql_real_escape_string($_REQUEST['ID']);
// Check the Course ID exists
$courseCheck = mysql_query("SELECT * FROM Courses WHERE CourseID = '".$ID."'");
if (mysql_num_rows($courseCheck) == 1) {
$checkMember = mysql_query("SELECT * FROM CourseMembers WHERE CourseID = '".$ID."' AND UserID = '".$_SESSION['UserID']."'");
if (mysql_num_rows($checkMember) == 1) {
?>
<html>
<head>
<!-- Style Sheets -->
<link rel="stylesheet" href="style/reset.css" type="text/css" media=screen />
<link rel="stylesheet" href="style/style.css" type="text/css" media=screen />
</head>
<body>
<?php
if ($_SESSION['LoggedIn'] == 1){
$courseInfo = mysql_query("SELECT * FROM Courses WHERE CourseID = '".$ID."'");
$row = mysql_fetch_assoc($courseInfo);
?>
<div id="container">
<div id="side">
<?php include("lib/sidebar.php"); ?>
</div>
<div id="main">
<div id="mainbox">
<div id="header"><b><?php echo $row['CourseName']; ?></b></div>
<p>Hello world, this is a test.</p>
</div>
</div>
</div>
<div class="clear"></div>
<?php
}
else {
echo "Not logged in.";
}
}
else {
echo "You are not a member of this Course";
}
}
else {
echo "No Course Found";
}
?>
</body>
I think they're already global. "PHP does not have a block-level scope."
You could store them in session variables, similarly to your $_SESSION['LoggedIn']
You could also use php variable $GLOBALS for making your variables visible in all scopes but I would not recommend it for this kind of task. Also, beware - $GLOBALS contains superglobals like $_POST and $_GET, you should keep that in mind, when ie. iterating over it. Furthermore - when you can access $_GET and $_POST in functions, that have smaller scope you still have to use $GLOBALS to access custom ones.
Example for this kind behaviour:
<?php
error_reporting(-1);
$GLOBALS['_customVar'] = 'foobar';
$GLOBALS['_GET']['id'] = 'myId';
function myFnc() {
echo $_customVar;
}
function myFnc2() {
echo $_GET['id'];
}
myFnc();
myFnc2();
?>

Categories