Cookies not saving when using if isset $_SESSION - php

Sorry for asking this since I am fairly new to PHP, but I am trying to create a login system wherein a user logs in to enter a homepage. The login page sets cookies for the username and password if the user checks the Remember me checkbox regardless if login is successful or not. If login is successful, it starts a session. While there is an active session, the user cannot access the login page unless they log out. If login is not successful, it will redirect back to the login page. My problem here is that the cookies are not set regardless if the login is successful or not. But if I remove the if isset($_SESSION) statement, it successfully sets the cookies. Below is my code:
login.php:
<?php
session_start();
if(isset($_POST["login"]))
{
$remember = $_POST["remember"];
if($remember == 1)
{
setcookie("username", $_POST["uname"], time()+86400);
setcookie("password", $_POST["pwd"], time()+86400);
}
if(isset($_SESSION['uname']))
{
echo"<script>alert('Already logged in.')</script>";
echo"<script>location.href = 'home.php'</script>";
}
}
?>
<html>
<head>
<title>Login</title>
<style>
body
{
background-color: DimGray;
}
table
{
font-family: Calibri;
color:white;
font-size: 14pt;
font-style: normal;
font-weight: bold;
text-align: left;
background-color: DimGray;
border-collapse: collapse;
border: 2px solid white
}
table.inner
{
border: 0px
}
.my_text
{
text-align: center;
font-family: Helvetica;
color:white;
font-size: 40px;
font-weight: bold;
margin: 50px;
}
</style>
</head>
<body>
<div class = my_text> Login Page</div>
<table align="center" cellpadding = "10">
<!----- Userame ---------------------------------------------------------->
<form method="post" action="home.php">
<tr>
<td>Username: </td>
<td><input type="text" name="uname" value = "<?php if(isset($_COOKIE["username"])) echo $_COOKIE["username"]?>"/>
</td>
</tr>
<!----- Password ---------------------------------------------------------->
<tr>
<td>Password: </td>
<td><input type="text" name="pwd" value = "<?php if(isset($_COOKIE["password"])) echo $_COOKIE["password"]?>"/>
</td>
</tr>
<!----- Remember me ------------------------------------------------->
<tr>
<td>Remember Me </td>
<td><input type="checkbox" name="remember" value = "1"/>
</td>
</tr>
<!----- Submit ------------------------------------------------->
<tr>
<td colspan="2" align="center">
<input type="submit" value="Login" name="login">
</td>
</tr>
</table>
</form>
<br>
<br>
</body>
</html>
home.php:
<?php
$uname = "admin";
$pwd = "12345";
session_start();
if(isset($_SESSION['uname']))
{
echo"<table align='center' cellpadding = '10'>";
echo"<tr><td>Welcome " . $_SESSION['uname'] . "</td></tr>";
echo"<tr align = 'center'><td><a href = 'logout.php'><input type = button value = logout name logout></a></td></tr>";
}
else
{
if($_POST["uname"] == $uname && $_POST["pwd"] == $pwd)
{
$_SESSION["uname"] = $uname;
echo"<script>location.href = 'home.php'</script>";
}
else
{
echo"<script>alert('Username or Password is incorrect.')</script>";
echo"<script>location.href = 'login.php'</script>";
}
}
?>
<html>
<head>
<title>Home</title>
<style>
body
{
background-color: DimGray;
}
table
{
font-family: Calibri;
color:white;
font-size: 30pt;
font-style: normal;
font-weight: bold;
text-align: left;
background-color: DimGray;
border-collapse: collapse;
border: 2px solid white
}
table.inner
{
border: 0px
}
.my_text
{
text-align: center;
font-family: Helvetica;
color:white;
font-size: 40px;
font-weight: bold;
margin: 50px;
}
</style>
</head>
<body>
<div class = my_text> Home Page</div>
</body>
</html>
logout.php:
<?php
session_start();
if(isset($_SESSION['uname']))
{
session_destroy();
echo"<script>location.href = 'login.php'</script>";
}
else
{
echo"<script>location.href = 'login.php'</script>";
}
?>

Related

need to show page only if a user logged in php

Sorry for a duplicate question, but its not working for me;
access page only if logged in with php
i have to access display.php page if someone loggedin, if they enter the url directly it need to redirect to login.php page,
i tried sessions but its not working for me, please help me to debug it.
display.php
<?php
session_start();
if(!isset($_SESSION['loggedin']))
{
header("location: login.php");
}
$conn=mysqli_connect("localhost","root","zaq12345","testdb");
if(!$conn)
{
die("Connection failed: " . mysqli_connect_error());
}
$disp = "select * from formdata order by user_id desc";
$result = mysqli_query($conn,$disp);
?>
<button onclick="location.href='formnew1.html';">Add</button>
<table border="2" cellpadding="0" cellspacing="0">
<tr>
<th> ID </th>
<th> Name </th>
<th> Email </th>
<th> Age </th>
<th> Gender </th>
<th> Address </th>
<th> City </th>
<th> Skills </th>
<th>Action</th>
</tr>
<?php
//$rows = mysqli_fetch_assoc($result);
while ($row = $result->fetch_assoc())
{
$id = $row['user_id']; ?>
<tr>
<td><?php echo $row['user_id']?> </td>
<td><?php echo $row['name'] ?></td>
<td><?php echo $row['email']?></td>
<td><?php echo $row['age']?></td>
<td><?php echo $row['gender']?></td>
<td><?php echo $row['address']?></td>
<td><?php echo $row['city']?></td>
<td><?php echo $row['skill']?></td>
<td>
<a id="edit" href="edit1.php?id=<?php echo $row['user_id']; ?>">Edit</a>
Delete
</td>
</tr>
<?php } ?>
</table>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.0/jquery.min.js"></script>
<script>
function deleteRow(obj){
conf=confirm('Are you sure to delete the Data');
if(conf){
var tr = $(obj).closest('tr');
$.post("delete1.php", {id: obj.id}, function(result){
tr.fadeOut('slow', function(){
$(obj).remove();
});
});
}
}
</script>
<?php
if (isset($_SESSION['success']))
{
echo '<script> alert("Data Added Successfully");</script>';
}
else if (isset($_SESSION['fail'])){
echo '<script> alert("Failed to Store");</script>';
//header("Location: /training/formnew.html");
}
mysqli_close($conn);
?>
login.php
<?php session_start(); ?>
<!DOCTYPE html>
<html>
<head>
<style>
div {
color: rgb(255,0,0);
}
form {
max-width: 425px;
margin: 10px auto;
padding: 10px 20px;
background: #ff994580;
border-radius: 10px;
}
fieldset {
margin-top: 100px ;
margin-bottom: 500px;
border: none;
}
h2 {
margin: 0 0 30px 0;
text-align: center;
font-family: 'Calibri';
font-size: 40px;
font-weight: 300;
}
label {
font-family: 'Calibri';
font-size: 16px;
font-weight: 50;
}
.submit {
background-color: #4CAF50;
border-radius: 10px;
color: white;
padding: 10px 40px 10px;
text-align: center;
font-size: 16px;
cursor: pointer;
}
.reset {
background-color: #ff3333;
border-radius: 10px;
color: white;
padding: 10px 40px 10px;
text-align: center;
font-size: 16px;
cursor: pointer;
}
</style>
</head>
<body>
<fieldset>
<form id="myform" name="myform" method="POST" action="validate.php">
<H2> LOGIN </H2>
<table width="60%" cellpadding="10">
<tr>
<td>
<label>User ID</label>
</td>
<td>
<input type="text" id="user_id" name="user_id"placeholder="Enter your User ID" required="required"/>
</td>
</tr>
<tr>
<td>
<label>User Name</label>
</td>
<td>
<input type="text" id="user_name" name="user_name" placeholder="Enter your Username" required="required"/>
</td>
</tr>
<tr>
<td>
<label>Password</label>
</td>
<td>
<input type="password" id="password" name="password" placeholder="Enter your Password" required="required"/>
</td>
</tr>
<tr>
<td>
<input type="submit" class="submit" name="submitbtn" value="Login">
</td>
<td>
<input type="reset" class="reset"/>
</td>
</tr>
</table>
</form>
</fieldset>
</script>
</body>
</html>
validate.php
<?php
session_start();
$conn=mysqli_connect("localhost","root","zaq12345","testdb");
if($_SERVER['REQUEST_METHOD'] == 'POST')
{
$userid=$_POST['user_id'];
$username=$_POST['user_name'];
$password=$_POST['password'];
$qz = "SELECT * FROM userdata WHERE user_id = '$userid' AND user_name = '$username' AND password = '$password'";
$result=mysqli_query($conn,$qz);
if(mysqli_num_rows($result) == 1 )
{
$_SESSION['loggedin'] = true;
$_SESSION['user_id'] = $userid;
header('location: display1.php');
}
else{
$_SESSION['loggedin'] = false;
echo '<script> alert("ERROR: Please Check Credentials OR SignUp!!!"); window.location.href="login.php"; </script>';
}
}
mysqli_close($conn);
?>
Either remove the line $_SESSION['loggedin'] = false; in your validate.php file.
Or change the if statement in your display.php file to be
if (!isset($_SESSION['loggedin'] || !$_SESSION['loggedin'])
You are setting the loggedin to be false, so when you call isset it returns true, because it is set even though it is set to false.

Database unable to insert value

when ever i am insert the value it says check data base connection. All the things are correct but it always giving the same error.
I am amking the quiz this page is for adding the question adding. whenever i am inserting the value it says please check DB connection. All the things are correct. User name password database name are also correct.
But same error is occuring
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>!deal Banker</title>
<style type="text/css">
body{
margin:0;
padding:0;
line-height: 1.5em;
}
b{font-size: 110%;}
em{color: red;}
#topsection{
background: #EAEAEA;
height: 90px; /*Height of top section*/
}
#topsection h1{
margin: 0;
padding-top: 15px;
}
#contentwrapper{
float: left;
width: 100%;
}
#contentcolumn{
margin: 0 200px 0 230px; /*Margins for content column. Should be "0 RightColumnWidth 0 LeftColumnWidth*/
}
#leftcolumn{
float: left;
width: 230px; /*Width of left column*/
margin-left: -100%;
background: #C8FC98;
}
#rightcolumn{
float: left;
width: 200px; /*Width of right column*/
margin-left: -200px; /*Set left marginto -(RightColumnWidth)*/
background: #FDE95E;
}
#footer{
clear: left;
width: 100%;
background: black;
color: #FFF;
text-align: center;
padding: 4px 0;
}
#footer a{
color: #FFFF80;
}
.innertube{
margin: 10px; /*Margins for inner DIV inside each column (to provide padding)*/
margin-top: 0;
}
/* ####### responsive layout CSS ####### */
#media (max-width: 840px){ /* 1st level responsive layout break point- drop right column down*/
#leftcolumn{
margin-left: -100%;
}
#rightcolumn{
float: none;
width: 100%;
margin-left: 0;
clear: both;
}
#contentcolumn{
margin-right: 0; /*Set margin to LeftColumnWidth*/
}
}
#media (max-width: 600px){ /* 2nd level responsive layout break point- drop left column down */
#leftcolumn{
float: none;
width: 100%;
clear: both;
margin-left: 0;
}
#contentcolumn{
margin-left: 0;
}
}
</style>
</head>
<body>
<div id="maincontainer">
<div id="topsection"><div class="innertube">
<?php
include'menu.php';
?></div></div>
<div id="contentwrapper">
<div id="contentcolumn">
<?php
$conn = mysql_connect("localhost","banker","gaurav#441");
$db = "idealbanker";
$table = "pp";
mysql_select_db($db,$conn);
if(isset($_REQUEST['submit']))
{
if($_POST['question'] == '')
{
echo 'cannot submit field empty';
}
elseif($_POST['answer1'] == '')
{
echo 'cannot submit field empty';
}
elseif($_POST['answer2'] == '')
{
echo 'cannot submit field empty';
}
elseif($_POST['answer3'] == '')
{
echo 'cannot submit field empty';
}
elseif($_POST['answer4'] == '')
{
echo 'cannot submit field empty';
}
elseif($_POST['answer5'] == '')
{
echo 'cannot submit field empty';
}
else
{
function clean($str) {
$str = #trim($str);
if(get_magic_quotes_gpc()) {
$str = stripslashes($str);
}
return mysql_real_escape_string($str);
}
$question = clean($_POST['question']);
$answer1 = clean($_POST['answer1']);
$answer2 = clean($_POST['answer2']);
$answer3 = clean($_POST['answer3']);
$answer4 = clean($_POST['answer4']);
$correctanswer = clean($_POST['answer5']);
$qry = "INSERT INTO $table( question, answer1, answer2, answer3, answer4, correctanswer )VALUES('$question','$answer1','$answer2','$answer3','$answer4','$correctanswer')";
$result = #mysql_query($qry);
if(!$result)
{
echo 'Question Cannot Submit Check DB Connection';
echo "<br>Add Again";
}
else
{
echo 'Question Submitted Successfully';
}
echo "<br>Add More";
}
}
else
{
echo '<form name="form1" method="post" action="'.$_SERVER['PHP_SELF'].'">
<table width="500" border="0">
<tr>
<td width="100">Question</td>
<td width="242">
<input name="question" type="text" size="60">
</td>
</tr>
<tr>
<td>Answer 1 </td>
<td><input name="answer1" type="text" size="30"></td>
</tr>
<tr>
<td>Answer 2 </td>
<td><input name="answer2" type="text" size="30"></td>
</tr>
<tr>
<td>Answer 3 </td>
<td><input name="answer3" type="text" size="30"></td>
</tr>
<tr>
<td>Answer 4 </td>
<td><input name="answer4" type="text" size="30"></td>
</tr>
<tr>
<td>Correct Answer </td>
<td><input name="answer5" type="text" size="30"></td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
<tr>
<td> </td>
<td><label>
<input type="submit" name="submit" value="Submit">
</label></td>
</tr>
</table>
</form>
';
}
?>
</div>
</div>
<div id="leftcolumn">
<?php
include 'left.php';
?>
</div>
<div id="rightcolumn">
<?php
include 'right.php';
?>
</div>
<div id="footer"><?php
include'footer.php';
?></div>
</div>
</body>
</html>
Your "check db connection" error message is UTTERLY useless. Never output a fixed unchanging message. Have the DB TELL you why the query failed:
if (!$result) {
die(mysql_error());
}
And note that you're simply ASSUMING the connection is working. You never bother checking the return values of your connection and select_db calls.

Parse error: syntax error, unexpected end of file in ?/login.php on line X [duplicate]

This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 7 years ago.
I am getting this error:
Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in ?/config.php on line 9
Code:
<!-- Config.php Code -->
<?php session_start();
//mysql connection
$con = mysql_connect("localhost","digmoorc","community20");
mysql_select_db("digmoorc_EHUB",$con);
function getUserData($userID) {
$query=mysql_query("select 'userID' from tbl_users where userID=$userID limit 1");
while($row = mysql_fetch_array($query))
if ($query === false) mysql_error();
{
}
}
?>
try this
<?php
require("config.php");
$error = '';
if(isset($_POST['Submit'])) {
$userName=$_POST['userName'];
$passWord=$_POST['passWord'];
$query=mysql_query("select user ID from tbl_users where userName='$userName' and passWord='$passWord' limit 1");
if(mysql_num_rows($query)==1) {
//login success
$data=mysql_fetch_array($query,1);
$_SESSION['userID']=$data['userID'];
header("location:dashboard.php");
exit();
}else{
//login failed
$error="Invalid Login";
}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<html>
<head>
<head profile="http://www.w3.org/2005/10/profile">
<link rel="icon" type="image/png" href="#" />
<!--META DATA -->
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta http-equiv="Content-Language" content="en" />
<link rel="credits" type="rel" href="http://www.cultivatecreative.co.uk/" />
<!-- STYLESHEETS -->
<style>
body {
background: #e8f5f6;
font-family: Arial, Helvetica, sans-serif;
font-size: 12px;
text-rendering: optimizeLegibility;
font-weight: normal;
color: #444;
padding-top: 39px;
-webkit-tap-highlight-color: #62A9DD;
}
#header {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 39px;
padding: 0;
background: #59c5c4;
-moz-box-shadow:inset 0 0 10px #2f6b8f;
-webkit-box-shadow:inset 0 -5px 10px #2f6b8f;
box-shadow:inset 0 0 10px #2f6b8f;
}
#wrapper {
width: 540px;
margin: 100px auto;
padding: 30px;
background: #fff;
}
a.logo {
margin: 18px auto 30px;
display: block;
width: 244px;
height: 107px;
background: url(file:///C|/Users/Daniel/Downloads/Evermore%20HUB/evermoorhub-logo.png) no-repeat;
text-indent: -9999px;
border: none;
}
h1 {
text-align: center;
font-size: 2.833em; /* 22px */
font-family: arial, sans-serif;
color: #444;
font-weight: normal;
color: #59c5c4;
}
a {
color: #000;
text-decoration: none;
}
a:hover {
border-bottom: none;
}
p {
text-align: center;
font-size: 1em; /* 22px */
font-family: arial, sans-serif;
color: #000;
font-weight: normal;
color: #000;
}
#wrapper table {
background-color: #59c5c4;
}
</style>
<title>Evermoor HUB | Login</title>
</head>
<body>
<div id="header"></div>
<div id="wrapper">
<a class="logo" href="http://www.evermoorhub.co.uk" title="Webpage design:">Daniel Woods</a>
<h1><strong><center>User Login</center></strong></h1>
<form action="" method="post">
<table width="100%" border="0" cellpadding="3" cellspacing="1" class="table">
<?php if(isset($error)); ?>
<td colspan="2" align="center"><strong class="error"><?php echo $error; ?></strong></td>
</tr>
<td width="50%" align="right">Username:</td>
<td width="50%"><input name="userName" type="text" id="userName"></td>
</tr>
<tr>
<td align="right">Password:</td>
<td><input type="password" name="passWord" id="passWord"></td>
</tr>
<tr>
<td align="right"> </td>
<td><input type="submit" name="Submit" id="Submit" value="Submit"></td>
</tr>
</table>
</form>
</div> <!-- Wrapper -->
</body>
</html>
In such cases you have to look for missing semicolons at the end of lines and for missing closing brackets.
In this case your first if clause
if(isset($_POST['Submit'])) {
is not closed. So there is no closing }.

Login page for php

I am trying to make a login page using php. My registration page is working already but there are errors in the login.
This is my first file.
<!DOCTYPE html>
<body>
<body background="sky.jpg"></body>
<div style="text-align: center;">
<div style="box-sizing: border-box; display: inline-block; width: auto; max-width:
480px; background-color: #FFFFFF; border: 2px solid #D4D4D4; border-radius: 5px; box-
shadow: 0px 0px 8px #D4D4D4; margin: 50px auto auto;">
<div style="background: #D4D4D4; border-radius: 5px 5px 0px 0px; padding: 15px;">
<span style="font-family: verdana,arial; color: #D4D4D4; font-size: 1.00em; font-
weight:bold;">HEY REGISTER NOW!</span></div>
<div style="background: ; padding: 15px">
<style type="text/css" scoped>
td { text-align:left; font-family: verdana,arial; color: #000000; font-size:
1.00em; }
input { border: 1px solid #CCCCCC; border-radius: 5px; color: #666666; display:
inline-block; font-size: 1.00em; padding: 5px; width: 100%; }
input[type="button"], input[type="reset"], input[type="submit"] { height: auto;
width: auto; cursor: pointer; box-shadow: 0px 0px 5px #D4D4D4; float: right; margin-
top: 10px; }
table.center { margin-left:auto; margin-right:auto; }
.error { font-family: verdana,arial; color: #000000; font-size: 1.00em; }
</style>
<form name="form1" method="post" action="checklogin.php">
<input type="hidden" name="action" value="login">
<input type="hidden" name="hide" value="">
<table class='center'>
<tr><td>Username:</td><td><input type="text" name="username"></td></tr>
<tr><td>Password:</td><td><input type="password" name="password"></td></tr>
<tr><td> </td><td><input type="submit" value="Enter"></td></tr>
<tr><td colspan=2> </td></tr>
<tr><td colspan=2>Don't have an admin account yet? Click here!</td></tr>
<tr><td colspan=2>Thank you for using Sentence Scramble and Sequencer</td></tr>
</table>
</form>
</div></div></div>
Followed by my checklogin
<?php
// Connect to server and select databse.
$server = 'localhost';
$username = 'root';
$password = '';
$database = 'project';
mysql_connect($server,$username,$password) or die(mysql_error());
mysql_select_db($database) or die(mysql_error());
// username and password sent from form
$myusername=$_POST['myusername'];
$mypassword=$_POST['mypassword'];
$sql="SELECT * FROM 'register' WHERE username='$myusername' and
password='$mypassword'";
echo $sql;
$result=mysql_query($sql,$con);
// Mysql_num_row is counting table row
$count=mysql_num_rows($result);
if($count==1){
session_register("myusername");
session_register("mypassword");
header("location:login_success.php");
}
else {
echo "Wrong Username or Password";
}
?>
And lastly, a file for login_success
<?php
session_start();
if(!session_is_registered(myusername)){
header("location:index.php");
}
?>
<html>
<body>
Login Successful
</body>
</html>
'myusername' and 'mypassword' are variables that come from the table 'register' from my database
You need to change your form so that the username and password input fields match what your form handler script is attempting to validate. Your input fields should look like this:
<tr><td>Username:</td><td><input type="text" name="myusername"></td></tr>
<tr><td>Password:</td><td><input type="password" name="mypassword"></td></tr>
Your connection variable not set. Please set it firt. Havent initiated session_start(). Usage of session_register() is deprecated.
<?php
session_start()
// Connect to server and select databse.
$server = 'localhost';
$username = 'root';
$password = '';
$database = 'project';
$con = mysql_connect($server,$username,$password) or die(mysql_error());
mysql_select_db($database) or die(mysql_error());
// username and password sent from form
$myusername=$_POST['myusername'];
$mypassword=$_POST['mypassword'];
$sql="SELECT * FROM 'register' WHERE username='$myusername' and
password='$mypassword'";
echo $sql;
$result=mysql_query($sql,$con);
// Mysql_num_row is counting table row
$count=mysql_num_rows($result);
if($count==1){
$_SESSION['myusername'] = $myusername;
$_SESSION['mypassword'] = $mypassword;
header("location:login_success.php");
exit;
}
else {
echo "Wrong Username or Password";
}
?>
And lastly, a file for login_success
<?php
session_start();
if(!isset($_SESSION['myusername'])) {
header("location:index.php");
exit;
}
?>
<html>
<body>
Login Successful
</body>
</html>

Passed session variable displaying wrong info inside a lightbox

I am creating an application in which a folder gets created by the username, and inside that folder, another folder gets created dynamically called Profile_Pics.
I have used Ajax Script also.
Its just a bit of Facebook album type application. Now the problem is that, for this thing I am using session, and user can created other folders also say like "My_Pics" inside his folder. The folder is for storing photos.
The path of the created album inside username folder is like this
Candidate_Pics/sex/Username/album_name
Example:
If a male of username saz26 registers first, then the folder Profile_pics will be created by default.
Candidate_Pics/Male/saz26/Profile_pics
If he creates another album say My_Pics then:
Candidate_Pics/Male/saz26/My_pics
Where under My_Pics he can store other pics.
Now I have created this page, and I need to work with session. And also I use session variable.
Here's my photos.php file:
<?php
session_start();
ob_start();
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
<link rel="stylesheet" type="text/css" media="screen" href="CSS/main.css" />
<link rel="stylesheet" type="text/css" media="screen" href="CSS/style.css" />
<script type="text/javascript" src="AJAX/AjaxCreateAlbum.js"></script>
<script>
function showalbum()
{
document.getElementById('AlbumDiv').style.display = "block";
document.getElementById('fade').style.display = "block"
return false;
}
</script>
</head>
<body>
<div id="pictures">
<h2>Photos</h2>
<form id="picture_form" name="picture_form" method="post" action="javascript:getfolder(document.getElementById('picture_form'));">
<table align="center" width="650" cellpadding="0" cellspacing="0" border="0">
<tr>
<td width="50px" align="center"/>
<td width="100px" align="center">
<label for="album_name">Create Album::</label>
</td>
<td width="50px" align="center"/>
<td width="100px" align="center">
<input type="text" name="album_name" id="album_name" class="textfield" style="width: 100px;"/>
</td>
<td width="50px" align="center"/>
<td width="100px" align="center">
<input type="submit" name="submit" id="submit" class="button" value="Create Album" />
</td>
<td width="50px" align="center"/>
<td width="100px" align="center">
<div id="album_report"></div>
</td>
<td width="50px" align="center"/>
</tr>
<tr>
<td colspan="9" height="20px"/>
</tr>
</table>
<?php
$folder=array();
if(is_dir("Candidate_Pics/". "$_SESSION[sex]". "/" . "$_SESSION[logged_user]"))
{
$dir=opendir("Candidate_Pics/". "$_SESSION[sex]". "/" . "$_SESSION[logged_user]");
$nofiles=0;
while ($file = readdir($dir))
{
if ($file != '.' && $file != '..')
{
$nofiles++;
$files[$nofiles]=$file;
}
}
closedir($dir);
}
/* populate sample data */?>
<?php
/* how many columns */
$column_number='3';
/* html table start */
?><table border="1" cellspacing="5" cellpadding="5" width="650px" align="center"
><?php
$recordcounter=1; /* counts the records while they are in loop */
foreach($files as $record) {
/* decide if there will be new Table row (<TR>) or not ... left of division by column number is the key */
if($recordcounter%$column_number==1){ echo "<tr>"; }
?>
<td width="200px" align="center">
<?php echo $record;?><br/>
<?php
$_SESSION['album_name']="Candidate_Pics/$_SESSION[sex]/$_SESSION[logged_user]/$record";
echo "$_SESSION[album_name]"; ?>
<a href="javascript:void(0)" onClick="showalbum();" ><img src="Images/folder.png" width="200px"/></a><br/>
<input type="button" name="delete" id="delete" value="delete" class="button"/>
</td>
<?php
/* decide if there will be end of table row */
if($recordcounter%$column_number==0){ echo "</tr>"; }
$recordcounter++; /* increment the counter */
}
if(($recordcounter%$column_number)!=1){ echo "</tr>"; }
?></table>
</form>
</div>
<div id="AlbumDiv" class="white_content">
<table align="center" cellpadding="0" cellspacing="0" border="0" width="382px">
<tr>
<td height="16px">
<a href="javascript:void(0)"
onclick="document.getElementById('AlbumDiv').style.display =
'none';document.getElementById('fade').style.display='none'">
<img src="images/close-icon.png" style="border-style: none; border-color: inherit;
border-width: 0px; height: 17px; width: 16px;" align="right" /></a>
</td>
</tr>
<tr>
<td>
<?php echo "$_SESSION[album_name]";?>
</td>
</tr>
<tr>
<td height="16px"/>
</tr>
</table>
</div>
<div id="fade" class="black_overlay">
</div>
</body>
</html>
the problem is that the line
echo "$_SESSION[album_name]";
inside the foreach loop is giving rite result for each record(rather say folder),
but the line
echo "$_SESSION[album_name]";
inside the AlbumDiv(used for the lightbox),
is only giving the result with Profile_pics, i.e. "Candidate_Pics/Male/saz26/Profile_pics"
no matter which ever folder image i click...
following is the AjaxCreateAlbum.js script
var http_request = false;
function makePOSTalbum(url, parameters)
{
http_request = false;
if (window.XMLHttpRequest)
{
// Mozilla, Safari,...
http_request = new XMLHttpRequest();
if (http_request.overrideMimeType)
{
// set type accordingly to anticipated content type
//http_request.overrideMimeType('text/xml');
http_request.overrideMimeType('text/html');
}
}
else if (window.ActiveXObject)
{ // IE
try
{
http_request = new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
try
{
http_request = new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e) {}
}
}
if (!http_request)
{
alert('Cannot create XMLHTTP instance');
return false;
}
http_request.onreadystatechange = alertAlbumCreated;
http_request.open('POST', url, true);
http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
http_request.setRequestHeader("Content-length", parameters.length);
http_request.setRequestHeader("Connection", "close");
http_request.send(parameters);
}
function alertAlbumCreated()
{
if (http_request.readyState == 4)
{
if (http_request.status == 200)
{
//alert(http_request.responseText);
result = http_request.responseText;
document.getElementById('album_report').innerHTML = result;
}
else
{
alert(http_request.status);
}
}
}
function getfolder(obj)
{
alert("huhu");
var poststring = "album_name=" + encodeURI( document.getElementById("album_name").value );
alert(poststring);
makePOSTalbum('createalbum.php', poststring);
}
And following is the createalbum.php script:
<?php
session_start();
if(mkdir("Candidate_Pics/$_SESSION[sex]/$_SESSION[logged_user]/".$_POST['album_name']))
echo "done";
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>
<body>
</body>
</html>
And following is the stylesheet file:
.black_overlay
{
display: none;
position: fixed;
top: 0%;
left: 0%;
width: 100.7%;
height: 100%;
background-color: black;
z-index: 1001;
-moz-opacity: 0.8;
opacity: .80;
filter: alpha(opacity=80);
}
.white_content
{
display: none;
position: fixed;
top: 37%;
left: 32%;
width: 382px;
padding: 0px;
border: 0px solid #a6c25c;
background: url(loginpanel.png);
z-index: 1002;
overflow: auto;
}
.headertext
{
font-family: Arial, Helvetica, sans-serif;
font-size: 14px;
color: #f19a19;
font-weight: bold;
}
.textfield
{
border: 1px solid #a6c25c;
width: 100px;
border : 1px solid #999;
-moz-border-radius: 5px;
-webkit-border-radius: 5px;
-khtml-border-radius: 5px;
_border-radius: 5px;
}
.dropdown
{
border: 1px solid #a6c25c;
border : 1px solid #999;
border-radius: 5px;
}
.button2
{
background-color: #a6c25c;
color: White;
font-size: 11px;
font-weight: bold;
border: 1px solid #7f9db9;
width: 100px;
}
.button
{
zoom: 1;
background: url(button.png);
color: White;
font-size: 11px;
font-weight: bold;
border: 1px solid #7f9db9;
-khtml-border-radius: 5px;
-moz-border-radius: 5px;
-webkit-border-radius: 5px;
#border-radius: 5px;
_border-radius: 5px;
border-radius: 5px;
}
#loading{
text-align: center;
visibility: hidden;
}
#content{
color: #000000;
margin: 0 0 20px 0;
line-height: 1.3em;
font-size: 14px;
}
.text
{
border: 1px solid #a6c25c;
border : 1px solid #999;
-moz-border-radius: 5px;
-webkit-border-radius: 5px;
-khtml-border-radius: 5px;
_border-radius: 5px;
}
/******* TOP *******/
#topmenu{
margin: 0 0;
padding: 0 0 0 0;
background: url(button.png);
}
/******* /TOP *******/
/******* MENU *******/
#topmenu #menu_main{
float: left;
list-style-type: none;
margin: 0px 0 0px 0px;
}
#topmenu #menu_main li{
float: left;
text-transform: uppercase;
color: #000099;
margin-left: 50px;
margin-right: 50px;
}
#topmenu #menu_main li:hover{
color: #6fa5fd;
cursor: pointer;
}
/******* /MENU *******/
/******* FOOTER *******/
#footer{
background: #efefef;
border: 1px solid #d0ccc9;
padding: 5px;
color: #7e7e7e;
font-size: 11px;
text-align: right;
}
/******* /FOOTER *******/
If I'm reading this correctly, your session variable is being set multiple times within a foreach loop, and then later called. Every time you write to that session variable within a loop, it overwrites the previous value, so once that loop is done, the only value the session variable will hold is the very last one from the loop.
Luckily you can remedy that pretty easily, since you're writing a javascript function call in an <a> tag in the next line. You can change that to echo the album name (while you're still in the loop) as what will on the client side be a Javascript argument.
<a href="javascript:void(0)" onClick="showalbum();" ><img src="Images/folder.png" width="200px"/></a><br/>
Becomes:
<a href="javascript:void(0)" onClick="showalbum('<?php echo $_SESSION['album_name']; ?>');" ><img src="Images/folder.png" width="200px"/></a><br/>
Then all you have to do is change your showalbum function to accept an argument, and use it to set the content of the intended <td> element. For example:
function showalbum(album_name)
{
document.getElementById('AlbumDiv').style.display = "block";
document.getElementById('fade').style.display = "block"
document.getElementById('albumName').innerHTML = album_name; // this line added
return false;
}
And in the PHP,
<td>
<?php echo "$_SESSION[album_name]";?>
</td>
Becomes:
<td id="albumName"></td>

Categories