How Can I get Id value from the incoming url? - php

When users comming this link www.sitename.com/index.php?userid=1
But user dont need to login.
I want to do change some value for the ID =1 user at the database. How can I change value. When the user just come this url address ?
This is my index.php code: (that include some other codes about qr code)
<?php
require "header.php";
require_once 'phpqrcode/qrlib.php';
$path='img/';
$file = $path.uniqid().".png";
?>
<?php
echo 'Current PHP version: ' . phpversion();
?>
<main>
<div class="wrapper-main">
<section class="section-default">
<?php
if (!isset($_SESSION['userID'])) {
echo '<p class="login-status">You are logged out!</p>';
}
else if (isset($_SESSION['userID'])) {
$text="http://sitename.com/project/userid{$_SESSION["userID"]}";
echo '<p class="login-status">You are logged in!</p>';
QRcode::png($text,$file,'L',10,2);
echo "<center><img src='".$file."'></center> " ;
}
?>
</section>
</div>
</main>
<?php
require "footer.php";
?>
EDIT 1
When I try this I failed where is my fault ?
<?php
if(!empty($_GET['user_idx'])){
$user_idx=$_GET['user_idx'];
require_once 'dbh.inc.php';
$sql = "UPDATE users SET balance=balance-5 WHERE user_idx='$user_idx';";
mysqli_query($conn,$sql);
}
In the dbh.inc.php
$servername ="127.0.0.1";
$dBUsername="xx";
$dBPassword="xx";
$dBName="xx";
$conn= mysqli_connect($servername,$dBUsername,$dBPassword,$dBName);
if(!$conn){
die("Connection failed:".mysqli_connect_error());
}

Data being fetched from the URL directly may be accessed or retrieved via the GET method
For your URL www.sitename.com/index.php?userid=1, you may access the userid=1 part by using the code $_GET['userid'] // which will result into "1"
Now, if you want to change it, you may create a link then pass a variable or new value to it:
In your case:
$text="http://sitename.com/project?userid{$_SESSION["userID"]}";
//we will treat the "userid" as a variable now
//so for you to fetch it, use:
//$_GET['userid']

Related

Display a link only if user is an admin

I am very new to PHP and I am trying to make a registration form only for an admin(no need for other users). I want to show one of the menu nav ("Add photo") only to admins.
LOGIN.php:
<?php
include_once 'header.php';
$username = "Efren";
$password = "111";
if (($_POST['txt_uname_email'] == $username)&& ($_POST['txt_password'] == $password)) {
session_start();
$_SESSION['admin_is_logged'] = true;
echo '<script type="text/javascript"> window.open("homepage.php","_self");</script>';
}
This is the part of the header that I am trying to show only to admins:
<?php
if (isset($_SESSION['admin_is_logged']) && $_SESSION['admin_is_logged'] == true){
echo '<li>add photo</li>';
}
?>
</ul>
Right now “add photo” is hidden both to admin and other visitors.
You need to start session on every page you want access to $_SESSION variable. I saw your session_start is inside if statement. Just set it on top of every file (where you need session) and it should work.
Put
session_start();
on file beginning just after <?php

Trouble returning session data (user name)

I'm trying to integrate a php login script that I have working, but I can't seem to get simple php calls going on a page. On this user profile page, I want to simply have the user name displayed (mysql field is "name"). The user is logged in and the session carries through, but on this page, all I see is the text "Here is your profile info..." What might be wrong in the code to prevent the user name from displaying?
<?php
include_once('classes/check.class.php');
include_once('header.php');
if( protectThis("*") ):
if(!isset($_SESSION)) {
session_start();
}
if(isset($_SESSION['jigowatt']['name'])) {
echo "You're name is: " . $_SESSION['jigowatt']['name'];
}
?>
<br />
Here are is your profile info...
<?php
else :
?>
<div class="alert alert-warning">
<?php _e('Only signed in users can view what\'s hidden here!'); ?></div>
<?php
endif;
include_once('footer.php');
?>
For check session is set already use session_id() Also check you have set $_SESSION['jigowatt']['name'] already with empty()
if(session_id() == '') {
session_start();
}
if(!empty($_SESSION['jigowatt']['name'])) {
echo "You're name is: " . $_SESSION['jigowatt']['name'];
}
else {
echo 'username is empty';
}
You need to put session_start(); at the very top of the page. No white space can be put before that. Try if that works.
First you need to write the sessions at the very top of the page if it works than okay else you can try this.
Just append this 2 function before and after the session_start();
Like this
ob_start();
session_start();
ob_end_clean();

Changing Index Page According to Login

Im creating an website where i am checking for login and redirecting the user to the index page, if his login was successful i want him to see something else instead of the login button
i have followed this approach for my query
<?php
if(!isset($_SESSION['uid']))
{
?>
<span class="Login">Login</span>
<?php
}
else if(isset($_SESSION['uid']))
{
?>
<span>Post</span>
<?php
}
?>
it doesn't seem to work quite the way i want. The 'Login' span is always visible, it would seem that the $_SESSION['uid'] is not being set, but that is not the case. To be honest i don't even know if this is the correct way of doing this
You need to put session_start(); in each page that need to access the session data before accessing (or creating) any session data.
See: Session Manuel
<?php
session_start();
$linkPage = 'login.php';
$linkName = 'Login';
if(isset($_SESSION['uid'])) {
$linkPage = 'postThread.php';
$linkName = 'Post';
}
?>
<span class="link"><?php echo $linkName; ?></span>

Variable $_SESSION does not work PHP

I want to add a simple "login/logout" script to my web site but it does not work.
<?php if(isset($_POST["signin"])){
session_start();
$username=stripslashes($_POST["username"]);
$password=stripslashes($_POST["password"]);
$username = mysql_real_escape_string($username);
$password = mysql_real_escape_string($password);
$nom=checkUser($username, $password);
if(!$nom=="")
{
$_SESSION['name'] = $nom;
header("location:account.php");
}
else {
echo 'WRONG USERNAME OR PASSWORD';}
}?>
the script above is header.php which means it's included in every single page; now here is the page of "account.php"
<?php if(isset($_SESSION['name']))
{
include('header.php');
echo'
</article>
<article class="col1 pad_left1">
<p>Bienvenue '.$_SESSION['name'].'</p>
</article>
</header>
</div>';
include('footer.php');}
header("location:index.php");
?>
The problem is that i always get to the index.php even if i'm logged in as if this test if(isset($_session['name'])) is always false.
I guess you rather want to use if($nom!="") than if(!$nom==""). Additionally, you need to call session_start() before you can use $_SESSION (you're doing it the other way round at the moment).
you have to start session in every page at the top by
session_start();
probably you are missing this.
Try to add session_start(); before if(isset($_SESSION['name'])) and check if it's a blank lines in your files at the top and in the end.

PHP variable scope question

I'm a beginner to PHP and trying to write a code, that does form validation.
It's nothing fancy just testing it out. I just wrote a function that test the Age input text whether it's a number or not. If it isn't it stores an error in an array and then display that error in another page. I saw this method in a video tutorial , but couldn't do it myself. When i try to invoke that error (not numeric value in Age) it always shows me this error in my add.php page :
Notice: Undefined variable: errors in /home/rafael/www/RofaCorp/add/add.php on line 37
How do i declare a variable that can be accessed through my whole project ?
Here's my code :
form_validation.php
<?php
function validate_number($number) {
global $errors;
if (is_numeric($number)) {
return $number;
}else {
$errors[] = "Value must be number";
}
if (!empty ($errors))
{
header("Location: ../add.php");
exit;
}
}
?>
create_ind.php
<?php require_once '../../include/connection.php'; ?>
<?php require_once '../../include/form_validation.php'; ?>
<?php require_once '../../include/functions_database_infoget.php'; ?>
<?php
$family_id = get_family_info_fam("id");
$ind_name = mysql_real_escape_string($_POST["ind_name"]);
$age = validate_number(mysql_real_escape_string($_POST["age"]));
$gender = $_POST["gender"];
$notes = mysql_real_escape_string($_POST["notes"]);
$add_query = "INSERT INTO individual
( g_id , ind_name , age , gender , notes)
Values ( {$family_id} , '{$ind_name}' , {$age} , '{$gender}' , '{$notes}')";
if(mysql_query($add_query , $connection)){
header("Location: ../../main.php");
exit;
} else {echo "ERROR " . mysql_error() ; }
?>
<?php mysql_close($connection); ?>
add.php (a portion of my code)
<!--Main Content-->
<section id="mainContent">
<header>
<h3>Select where to add new Family/Individual.</h3>
</header>
<article>
<?php
if (count($errors) > 0) {
for($i=0 ; $i < count($errors) ; $i++){
echo "{$errors[$i]}" . "<br/>";
}
}
?>
</article>
</section>
A global variable is only defined while your scripts are processing, as soon as you do header("Location: ../add.php"); you are loading a new page and all variables are lost. That´s what the error message is telling you, there is no variable $errors in add.php.
If you want your error message to persist between different page loads, a session variable is a good option (there are of course others like databases, etc.). Just start the session again in add.php and you have access to the variables stored in the session.
If it is shown in another page, use sessions.
Will allow you to retrieve variables from other pages.
You can use this simple tut http://www.tizag.com/phpT/phpsessions.php

Categories