Send an array object for $ _SESSION php

Technology

header ads

Send an array object for $ _SESSION php


Send an array object for $ _SESSION php


In our MBA course in java Many times we find that in the login of our project, especially when starting the section, we must send some parameters to get the data entered and verified in the user record to save them in a section in php, and use them later in our project in java but it turns out that it is unorthodox and ordinary to have to call or save several $ _SESSION or sections knowing that we can use an arrangement in php, a bad programming tendency is to use it in the following way, consulting in the base of data an example so to speak:

Send an array object for $ _SESSION php and use it in another class by calling the row



$_POST['userlogin']=$row['user_login'];
$_POST['useremail']=$row['user_email'];
$_POST['username']=$row['user_name'];
$_POST['userphone']=$row['user_phone'];

echo 'etc...etc... =)';

 


The above is a shame for programming should not use it, for that there are the arrays or programming vectors a way to use it in our project is like this:

1. obviously we must load the start section in our project:



 

session_start();
 


Where we are going to use the $ _SESSION we must add the code above hopefully in the first lines of php, now suppose that you already have the configuration and the connection with the database

now once the users enter by means of the login in the part where the section is saved, let's take as validadar.php that comes after the login.php where you enter the data in this way:




 $lasentencia="select * from mytable";
  $sql= mysqli_query($link,$lasentencia);

  


where mytable is the name of your table and  $link is the result of  @mysqli_connect connection.php:



 
$link = @mysqli_connect($db_host,$db_usuario,$db_password,$db_nombre)
        or die(mysqli_connect_error($link));
 




now we only search the row with a condition that if there are results of that query:



 
if($row = mysqli_fetch_array($sql)){
$_SESSION['objLogin'] = $row;

}
 


$_SESSION['objLogin'] = $row; we save it in this object as an array or vector here all the data of the query will be saved name, surname, telephone etc ...


with this we are sending an array to the section and ask it to save that vector to use it later, now after saving let's say we are in validar.php we use it in another class for example called content.php let's say we need user information as the name:





session_start();
if (isset($_SESSION['objLogin']['name'])) {//we validate that it does not arrive empty https://www.mbajava.com/
 echo 'your name is:
'.$_SESSION['objLogin']['name']; 

}else{
echo 'sorry not data';
}
  



and so we can consult each row of our database in a single line another way is to call it by means of its location of the vector example the first attribute or data of the vector in position 0 is name then we will call it like this:
 

session_start();
if (isset($_SESSION['objLogin'][0])) {//we validate that it does not arrive empty https://www.mbajava.com/

 echo 'your name is:
'.$_SESSION['objLogin'][0]; 

}else{
echo 'sorry not data';
}

 



and so we can call each one in a different way I hope I have helped them do not forget to subscribe to this page of masters of MBA programming leave a comment if I help you or any questions




Post a Comment