Question on redirection of pages using php - php

Code for page1.php-(Here named as index.php)
<?php
require_once "pdo.php";
session_start();
if(!(isset($_SESSION['email'])||isset($_SESSION['password']))
{
$url = "login.html";
}
?>
<html>
<head></head><body>
<?php
if ( isset($_SESSION['error']) ) {
echo '<p style="color:red">'.$_SESSION['error']."</p>\n";
unset($_SESSION['error']);
}
if ( isset($_SESSION['success']) ) {
echo '<p style="color:green">'.$_SESSION['success']."</p>\n";
unset($_SESSION['success']);
}
if (isset($_SESSION['name']))
echo('<table border="1">'."\n");
$stmt = $pdo->query("SELECT name, email, password, user_id FROM users");
while ( $row = $stmt->fetch(PDO::FETCH_ASSOC) ) {
echo "<tr><td>";
echo(htmlentities($row['name']));
echo("</td><td>");
echo(htmlentities($row['email']));
echo("</td><td>");
echo(htmlentities($row['password']));
echo("</td><td>");
echo('Edit / ');
echo('Delete');
echo("</td></tr>\n");
}
?>
</table>
<!--Add New-->
<!--Login-->
I have been given page1.php which is the first page when a user opens the application.
In page1.php, a link is given to login. This page redirects to login.php which requires us to login with genuine email and password.
After logging in, it returns to page1.php which has options like add new item to some database or delete/edit data in database and it also has an option of logout which then gives back the original page1.php in which we have been given the option of login.
The problem is if I do operations on database via another page (i.e. like page2.php) it is doable but I am not able to update page1.php after the login.php redirects to page1.php.
How do I work so that login.php page redirects to index.php with modified options? (which are operations on database.) Also how do I do this using href tags in html instead button operations?

Related

How to pass links with values to pahe2.php

This is a fragment of my code, that get all users from Active Directory (some fields). The first column (user_name) is a reference to the page2.php where I can see User profile more detail. The problem is page1.php passes only last values of the link from the cycle. How can I do that, any dynamic link of the user passes its information. Could you help me? Thanks. $info is an array of LDAP data.
Here is my code:
<?php for ($i=0; $i<$info["count"]; $i++) {
$res=$info[$i];
$m_name=$res["displayname"];
$m_title=$res["title"];
$m_dep=$res["department"];
$m_tel=$res["telephonenumber"];
$m_mail=$res["mail"];
?>
<?php echo '<div class="column"><h5>'.$m_name[0].'</h5></div>'?>
<?php echo '<div class="column"><h5>'.$m_title[0].'</h5></div>'?>
<?php echo '<div class="column"><h5>'.$m_tel[0].'</h5></div>'?>
<?php echo '<div class="column"><h5>'.$m_tel[0].'</h5></div>'?>
<?php echo '<div class="column"><h5>'.$m_mail[0].'</h5></div>'?>
<?php } ?>
Depending on exactly what you are trying to do, you can do it in two ways. Based on what you described, and your comment about Sessions, you can pass information from page to page using a session.
http://php.net/manual/en/session.examples.basic.php
session_start();
$_SESSION['displayname'] = $res["displayname"];
$_SESSION['title'] = $res["title"];
$_SESSION['department'] = $res["department"];
$_SESSION['telephonenumber'] = $res["telephonenumber"];
$_SESSION['mail'] = $res["mail"];
And then on Page2, read the values back or use them directly on the page:
$name = $_SESSION['displayname'];
or
echo 'Hello ' . $_SESSION['displayname'] . ' how are you?';
But if Page1 is a list of all users, how would you determine what information to store in the session for passing to Page2?
It would make more sense to me to use GET parameters to pass information from page to page. So, for example, if Page1 is a list of all users, and Page2 information about a single user, you could structure your loop like this:
<?php for ($i=0; $i<$info["count"]; $i++) {
$res=$info[$i];
$m_name=$res["displayname"];
$m_title=$res["title"];
$m_dep=$res["department"];
$m_tel=$res["telephonenumber"];
$m_mail=$res["mail"];
?>
<?php echo '<div class="column"><h5>'.$m_name[0].'</h5></div>'?>
<?php echo '<div class="column"><h5>'.$m_title[0].'</h5></div>'?>
<?php echo '<div class="column"><h5>'.$m_tel[0].'</h5></div>'?>
<?php echo '<div class="column"><h5>'.$m_tel[0].'</h5></div>'?>
<?php echo '<div class="column"><h5>'.$m_mail[0].'</h5></div>'?>
<?php } ?>
The unique ID from LDAP is just that. Something unique to that user that you can use to search on Page2 and pull out all of the details you want to print. It should be present in your $res array already.
So Page2 would have something like this at the top:
if(!isset($_GET['id']){
//Give an error if the ID isn't in the URL
}
$id = $_GET['id'];
$result = loadFromLDAP($id);
//Do stuff with the resulting $result array
loadFromLDAP being a function you would write to get details about a user from LDAP. Hope this helps get you on the right track.

How to store a value in a session?

I am displaying the user names as links on a php page like this which on clicking navigates on particular user home page:
$msql=$db->prepare("SELECT * from users where id=? order by id desc");
$msql->bind_param("i",$user_id);
$msql->execute();
$msql = $msql->get_result();
$msql = $msql->num_rows;
while($usercount=$msql->fetch_assoc())
{
$Email = $usercount['email'];
$FirstName = $usercount['first_name'];
$LastName = $usercount['last_name'];
?>
<strong><?php echo $FirstName.' '.$LastName;?></strong>
<?php
}
?>
And for the navigation to the user home page on clicking the link, I am using like this :
<?php
if(isset($_GET['navigate']) && $_GET['navigate'] == "true"){
$_SESSION['email'] = $Email;
header('location: nextpage.php');
}
?>
So my page looks like this as link
user1
user2
user3
.
.
.
usern
My issue is whenever I click on any of the links it always stores the first email in the session variable.
So, if I output the SESSION in nextpage.php
echo $_SESSION['email'];
it always echoes the first link email.
My guess for this because of the while loop I am using it always picks up the first link data and stays with it, but my question is how do i get the data for others as well. I want to navigate to that user page for which the link is clicked and that can only be done if I get the correct email on clicking the link.
As you said id is not primary key, and i assume that your same id will contain different emails, you have to do below changes:-
<?php
$msql=$db->prepare("SELECT * from users where id=? order by id desc");
$msql->bind_param("i",$user_id);
$result = $msql->execute(); // assign to a variable
//$msql = $msql->get_result(); //you are over-writing to one variable which is not correct
//$msql = $msql->num_rows; //you are over-writing to one variable which is not correct
while($usercount=$result->fetch_assoc()){
$Email = $usercount['email'];
$FirstName = $usercount['first_name'];
$LastName = $usercount['last_name'];
?>
<strong><?php echo $FirstName.' '.$LastName;?></strong> <!-- send mail id to the url too otherwise how's you will get the email id to save it into SESSION-->
<?php } ?>
AND
<?php
if(isset($_GET['navigate']) && $_GET['navigate'] == "true"){
$_SESSION['email'][] = $_GET['email']; // assign each mail to SESSION ARRAY not SESSION variable
header('location: nextpage.php');
}
?>
AND
echo "<pre/>";print_r($_SESSION['email']); //to see all emails.
Note:- You have to write session_start(); on top of your php page just after <?php if you want to work with SESSION on that page.

Remove item from session array

I am having trouble removing an item from the itinerary/array upon clicking a 'Remove location' link.
The value of the location to be removed is passed into the 'remove_loc.php' file as its unique 'loc_id' and assigned to a new variable. Upon doing this I then try to use 'unset()' to delete it from the session array but it doesn't seem to be working.
The value of the location is definitely being passed through to the 'remove_loc.php' file ok as I have run an echo statement at other end to make sure, and am connecting ok to db etc, so I believe the error is in my remove_loc.php file.
Code for itinerary.php (where locations are displayed).Note 'Remove Location' href link:
<?php
session_start();
include ("includes/db.php");
include("functions/functions.php");
include("includes/head.html");
include("includes/search_box.html");
include("includes/left_sidebar.html");
//If user not already logged in then redirect to the login page
if (!isset($_SESSION['user_id'])){
load_page();
}//if
//Display contents of itinerary
if(!empty($_SESSION['itinerary'])){
//Retrieve details of each location in array from database
$query = "SELECT * FROM locations WHERE loc_id IN (";
foreach ($_SESSION['itinerary'] as $loc_id=>$value)
{$query.=$loc_id.',';}
$query = substr($query, 0, -1).')ORDER BY loc_id ASC';
$result = mysqli_query($db, $query);
echo'<form action="itinerary.php" method="POST"><table><tr><th colspan="5">Locations in your itinerary<br></br></th></tr><tr>';
//Display locations in array
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)){
$loc_id = $row['loc_id'];
echo"<tr><td>{$row['loc_name']}<br></br></td>
<td>{$row['loc_desc']}</td>
<td><a href=remove_loc.php?value=$loc_id>Remove location</a><br></br></td>
";
}//while
mysqli_close($db);
}//if
else {echo '<p>Your itinerary is empty.<br></br></p>';}
echo '<p>
<a href=submit.php?submit=$submission>Save itinerary</a><br></br>
Your details<br></br>
Logout<br></br>
</p>';
?>
Code for 'remove_loc.php' (for removing location from session array):
<?php
session_start();
include ("includes/db.php");
include("functions/functions.php");
include("includes/head.html");
include("includes/search_box.html");
include("includes/left_sidebar.html");
?>
<html>
<h1>REMOVE LOCATION</h1>
<br></br>
<?php
//If user not already logged in then redirect to the login page
if (!isset($_SESSION['user_id'])){
load_page();
}//if
//Assign ID of location which has been passed to new variable
if(isset($_GET['value']))
$loc_id = $_GET['value'];
//Remove location from sess array?
if(isset($_SESSION['itinerary'][$loc_id])) unset($_SESSION['itinerary'][$loc_id]);
echo 'Removal successful!';
print_r($_SESSION);
?>
</html>

session id store on index page

this is login.php and its header location is index.php and i want to store session id on index page but nothing has been shown
<?php
#extract($_POST);
include("config.php");
$sql="select password,name,us.id as uid from us left join tblblo on (tblblo.maker=us.id) where name='$websitename' and password=md5('$password')";
$result=$db->query($sql);
if($count=mysql_num_rows($result)>0)
{
$row=mysql_fetch_assoc($result);
$_SESSION['name']=$row['name'];
$_SESSION['id']=$row['uid'];
header("location:http://".$_SESSION['name'].".domain.com");
}
else
{
//$_SESSION['sess_msg']="Invalid Web Name or Password!Try Again.";
//header("location:https://domain.com/login.php");
//exit;
}
?>
i want to store session id on index page but nothing has been shown
1) It's always good to write session_start(); in the very beginning of your code before outputting anything else..
<?php
session_start();
#extract($_POST);
include("config.php");
2) You're mixing up the deprecated mysql API's with the object_oriented ones.
Change this:
if($count=mysql_num_rows($result)>0)
{
$row=mysql_fetch_assoc($result);
$_SESSION['name']=$row['name'];
$_SESSION['id']=$row['uid'];
To this:
$count = $result->num_rows;
if($count > 0)
{
$row = $result->fetch_assoc());
$_SESSION['name']=$row['name'];
$_SESSION['id']=$row['uid'];
3) this is login.php and its header location is index.php and i want to store session id on index page but nothing has been shown
I think what you meant was to use/echo the session values on index.php rather store?
on your index.php (after successfully storing the sessions on login.php):
<?php
echo $_SESSION['name'];
echo $_SESSION['id'];
?>

issue in session variables

I am new to php.
I want to use form data that are sent through <form method="POST" action="formdata.php"> to formdata.php be used in another file called main.php. For this I ucerated session variables in formdata.php
Hers's my code in formdata.php
<?php
session_start();
include_once("connect.php");
$n=$_POST['name'];
$p=$_POST['password'];
$sql=mysql_query**strong text**("SELECT * FROM member WHERE `userName`='$n' AND `password`='$p'");
if(mysql_num_rows($sql)==1){
$_SESSION['user']=mysql_fetch_array(mysql_query("SELECT * FROM member WHERE `userName`='$n' AND `password`='$p'"));
if($_SESSION['user']){
$user=$_SESSION['user'];
$_SESSION['userN']="$user(['userName'])";;
$_SESSION['level']="$user(['level'])";
//header("location:mainPage.php");
echo $user['level'];
echo $_SESSION['level'];
}
}
else{
echo "invalid user name or password" ;
}
?>
But when I echo $user['level'];
echo $_SESSION['level']; all I get printed is 3Array(['level']). Here echo $user['level'] gives the desired out put 3, but echo $_SESSION['level'] gives an array as Array(['level']). What can I do to make it print 3?
My next question is I want to use this level value in another php file (main.php). I lerant that session variables are global. So can I use $user['level'] or should I use $_SESSION['level']. In main.php I want to check the condition
if($user['level'] == 3) {
echo "level 3 user";
}
The issue is this line: $_SESSION['userN']="$user(['userName'])";;
You're setting $_SESSION['userN'] equal to whatever $user is in string form which is Array, and (['username']).
Why are you even setting userN? $_SESSION should already contain the user in question, and you can just retrieve it anywhere to get the contents.
This should be all you need:
<?php
session_start();
include_once("connect.php");
$n=mysql_escape_string($_POST['name']);
$p=mysql_escape_string($_POST['password']);
$sql = mysql_query("SELECT * FROM member WHERE `userName`='$n' AND `password`='$p'");
if(mysql_num_rows($sql)==1)
{
//You can reuse the original $sql here. No need to run another query.
$_SESSION['user']=mysql_fetch_array($sql);
//If mysql_num_rows($sql) returns 1, $_SESSION['user'] should always be equal to a user array (Which is true).
header("location:mainPage.php");
}
else{
echo "invalid user name or password" ;
}
?>
On the other page (mainPage.php), just use session_start(), and check to see what is inside $_SESSION. This script should give you a jump start:
<?php
session_start();
print_r($_SESSION);
//This will get the level you're looking for
$level = $_SESSION['user']['level'];
?>
It should contain the user/level and any other information you need.

Categories