PHP session lost after redirecting the page

I was inserting some data and was trying to show my success message using SESSION variables in PHP. my code segment of samplepage.php was like this:

if(isset($_POST["Add"]))
{
    $txtName=$_POST['txtName'];
    $txtKey= $_POST["txtKey"];
         
    //when submit button is add
    if($btnValue=="Add")
    {
        if($txtName<>"") //Permission Name can't be blank
        {
            $query = "insert into Table(NAME, KEY) values('$txtName','$txtKey')" ;
            $st = oci_parse($conn, $query);                             
            $success = oci_execute($st,OCI_DEFAULT); // oci as db is oracle here
            oci_commit($conn);
             
            if($success ==1)
            {
            $_SESSION['MySuccessMsg'] = "Data Added Successfully!";
            header('Location: samplepage.php');
            } 
                
               }
         
        }
}

and below this segment I was catching the $_SESSION[‘MySuccessMsg’] in a div in top of my HTML body:

                if(isset($_SESSION['MySuccessMsg'])) // receiving SESSION message .
		{ $msg = $_SESSION['MySuccessMsg'];}				
                echo "<strong>".$msg1."</strong>";  } 
                unset($_SESSION['MySuccessMsg']); //unset session variable
?> 

so every time the page was redirected after $success==1 condition segment, I found no data in $msg.

Later I found a help and realized, it is of 2 cases:

1) make sure session_start() is at the top of the page.
2) use exit() right after where the page is redirected from.

so my new code segment of insert becomes:

     if($success ==1)
     {
	$_SESSION['MySuccessMsg'] = "Data Added Successfully!";
	header('Location: permission.php');
	exit(); // this one is important to keep the session variable hold data even after re-direct	
     }  

NOTE: even if we redirect the page, it still executes the code till the end. therefore my session variable was being unset. so, EXIT() function works here not to unset the variable and code stops execution and moves to the redirected page immediately.

thanks to: stackoverflow

How to avoid duplicate entry in each page reload after PHP submit

I was using form submit using method=’POST’ and action=$_SERVER[‘PHP_SELF’]. I added data in my DB successfully but whenever I manually reloaded my page, it was a duplicate entry. using POST variables carefully, I was unable to resolve the problem.

here goes my code segment of samplepage.php:


if(isset($_POST["Add"]))
{
	$txtName=$_POST['txtName'];
        $txtKey= $_POST["txtKey"];
		
	//when submit button is add
	if($btnValue=="Add")
	{
		if($txtName<>"") //Permission Name can't be blank
		{
		    $query = "insert into Table(NAME, KEY) values('$txtName','$txtKey')" ;
		    $st = oci_parse($conn, $query);								 
	            $success = oci_execute($st,OCI_DEFAULT); // oci as db is oracle here
		    oci_commit($conn);
			
		    if($success ==1)
		    {
		     $SuccessMessage = "Data Added Successfully!";
		    }  
               
               }
        
        }
}

Later, I realized that no matter what how I check the post variables and use them carefully, the variables holds data and in manual reload of the page they were being inserted.

The solution is to RE-DIRECT the page!!.

So, the code segment becomes:

if($success ==1)
		{
		   $SuccessMessage = "Data Added Successfully!";
                   header('Location: samplepage.php');
		} 

Difference between include() and include_once() Functions in PHP

When should I use require vs. include?

The require() function is identical to include(), except that it handles errors differently. If an error occurs, the include() function generates a warning, but the script will continue execution. The require() generates a fatal error, and the script will stop.

When should I use require_once vs. require

The require_once() statement is identical to require() except PHP will check if the file has already been included, and if so, not include (require) it again.

thanks to stackoverflow

localhost error :: connection Failed The system returned: (111) Connection refused

I installed my apache server in my PC, but yet, I couldnt load "localhost" . the error was. I was using Mozilla Firefox and IE both.

While trying to retrieve the URL: http://localhost/

The following error was encountered:

  • Connection Failed

The system returned:

 (111) Connection refused

Finally the solution I found was interesting!

  1. Go to the LAN settings (in Chrome : Tools-> Options-> Under the hood -> Change Proxy setting -> LAN Setting)
  2. There will be a checkbox for "Bypass proxy server for local address"
  3. Tick the checkbox.

Thanks to : this link

Opening a pop up window using Javascript

I was in need of a JS function of opening a pop up window. I found a wonderful tutorial about this from Here Here is the Javascript Function for POP UP window named popitup()


<script type="text/javascript">// 
function popitup(url) {
       newwindow=window.open(url,'name','scrollbars,height=500,width=500','scrollbars');
       if (window.focus) {newwindow.focus()}
       return false;
}
</script>

The interesting part is, the the window.open() function. The arguments of this function are mainly three. First one is URL, second is NAME. and in third one we can put many characteristics of the popup window. they will have to be in a single quotation mark and distinguished by comma(,) For example, see the above window.open function window.open(url,’name’,’scrollbars,height=500,width=500′). Here after url, name, all other scrollbars, height, width are taken in the single quotation marks.

More functionality can be added in this THIRD part of the argument of window.open() function:

dependent
directories
fullscreen
location
menubar
resizable
scrollbars
status
toolbar
top=200
left=400
width=200
height=200
screenX=400
screenY=200


 <a onclick="return popitup('popupex.html')" href="popupex.html">Link to popup</a>

sending php array variable as link to argument of javascript function

I have got a javascript function popitup() which will be used for opening a pop up window. I have PHP Array variable $row_rstest[‘id’] which holds the ID of a data set fetched from database.
I have a page named print_data.php. To view the data of this page the url should be like print.data?id=12
I have taken this in variable $url.

 <?php $url = "print_data.php?id=".$row_rstest['id']; ?>

Then passed this $url in function popitup(). The important thing is to put the sign (‘) around $url variable. Otherwise the function didn’t work for link.

  <?php echo "<a href=\"\" onClick=\"return popitup('$url') \">Click here to open a pop up window</a>"; ?>
          

Names of Images Saved in PHP Server Root Are Case Sensitive

I used some images in my PHP scripts. I typed the perfect names of images (eg chair.jpg) in my PHP script. Though in my web root directory, the images are stored by the same name, in output, I didn’t find the images.

Later I found that, the image was named like “checked.PNG” in my web root. But in my program, it was “checked.png”.
For having lower case in my code [png] but main image was in upper case [PNG], I didn’t find the image in output screen after loading.

This scenario doesn’t happen always. It varies! I found output the other time though the extension was different [one was in upper case and the other in lower case]