0

I am getting this error on hostgator Fatal error: Allowed memory size of 268435456 bytes exhausted (tried to allocate 64 bytes) but its working perfect on bluehost I do have access to php.ini and memory limit is already set to 64 memory_limit = 64M;

I am unable to figure it out that wht is it so, please help.

I don't know which part of code is generating error, but I am providing all of it.

config.php

    <?php
    $currency = '$'; //Currency sumbol or code

    $db_username = 'root';
    $db_password = '';
    $db_name = 'educators';
    $db_host = 'localhost';
    $mysqli = new mysqli($db_host, $db_username, $db_password,$db_name);
    ?>

cart.php

<div class="shopping-cart">
<h2>Your Cart</h2>
<?php
if(isset($_SESSION["courses"]))
{
$total = 0;
echo '<ol>';
foreach ($_SESSION["courses"] as $cart_itm)
{
    echo '<li class="cart-itm">';
    echo '<span class="remove-itm"><a href="cart_update.php?removep='.$cart_itm["id"].'&return_url='.$current_url.'">&times;</a></span>';
    echo '<h3>'.$cart_itm["title"].'</h3>';

    echo '<form action="cart_update.php" method="post">
    <div>No. of Participants : <input name="qty" class="qty" type="text" value="'.$cart_itm["qty"].'" />
    <input type="hidden" name="course_id" value="'.$cart_itm["id"].'" />
    <input type="hidden" name="return_url" value="'.$current_url.'" />
    <input class="update" name="update" type="submit" value="Update" />
    </div>
    </form>';

    echo '<div>Price :'.$currency.$cart_itm["fee"].', Subtotal: '.$currency.$cart_itm["fee"]*$cart_itm["qty"].'</div>';
    echo '</li>';
    $subtotal = ($cart_itm["fee"]*$cart_itm["qty"]);
    $total = ($total + $subtotal);
}
echo '</ol>';
echo '<span class="check-out-txt">Total : '.$currency.$total.'</span> <a class="checkout" href="view-cart.php">Next</a> <br class="clear" />';
}else{
echo 'Your Cart is empty';
}
?>
</div>

cart_update.php

<?php
include_once("config-cart.php");

//add item in shopping cart
if(isset($_POST["type"]) && $_POST["type"]=='add')
{
    $id     = filter_var($_POST["id"], FILTER_SANITIZE_STRING); //course code
    $return_url     = $_POST["return_url"]; //return url

    //MySqli query - get details of course from db using course code
    $results = $mysqli->query("SELECT title,fee FROM courses WHERE id='$id' LIMIT 1");
    $obj = $results->fetch_object();

    if ($results) { //we have the courses info 

        //prepare array for the session variable
        $new_courses = array(array('title'=>$obj->title, 'id'=>$id, 'qty'=>1, 'fee'=>$obj->fee));

        if(isset($_SESSION["courses"])) //if we have the session
        {
            $found = false; //set found item to false

            foreach ($_SESSION["courses"] as $cart_itm) //loop through session array
            {
                if($cart_itm["id"] == $id){ //the item exist in array
                    $qty = $cart_itm["qty"]+1; //increase the quantity
                    $courses[] = array('title'=>$cart_itm["title"], 'id'=>$cart_itm["id"], 'qty'=>$qty, 'fee'=>$cart_itm["fee"]);
                    $found = true;
                }else{
                    //item doesn't exist in the list, just retrive old info and prepare array for session var
                    $courses[] = array('title'=>$cart_itm["title"], 'id'=>$cart_itm["id"], 'qty'=>$cart_itm["qty"], 'fee'=>$cart_itm["fee"]);
                }
            }

            if($found == false) //we didn't find item in array
            {
                //add new user item in array
                $_SESSION["courses"] = array_merge($courses, $new_courses);
            }else{
                //found user item in array list, and increased the quantity
                $_SESSION["courses"] = $courses;
            }

        }else{
            //create a new session var if does not exist
            $_SESSION["courses"] = $new_courses;
        }

    }

    //redirect back to original page
    header('Location:'.$return_url);
}

//remove item from shopping cart
if(isset($_GET["removep"]) && isset($_GET["return_url"]) && isset($_SESSION["courses"]))
{
    $id     = $_GET["removep"]; //get the courses code to remove
    $return_url = $_GET["return_url"]; //get return url

    foreach ($_SESSION["courses"] as $cart_itm) //loop through session array var
    {
        if($cart_itm["id"]==$id){ //item exist in the list

            //continue only if quantity is more than 1
            //removing item that has 0 qty
            if($cart_itm["qty"]>1) 
            {
            $qty = $cart_itm["qty"]*0; //just decrese the quantity from -1 to *0
            //prepare array for the coursess session
            //$courses[] = array('title'=>$cart_itm["title"], 'id'=>$cart_itm["id"], 'qty'=>$qty, 'fee'=>$cart_itm["fee"]);
            }

        }else{
            $courses[] = array('title'=>$cart_itm["title"], 'id'=>$cart_itm["id"], 'qty'=>$cart_itm["qty"], 'fee'=>$cart_itm["fee"]);
        }

        //set session with new array values
        $_SESSION["courses"] = $courses;
    }

    //redirect back to original page
    header('Location:'.$return_url);
}
//remove item from shopping cart
if(isset($_POST["update"]) && isset($_SESSION["courses"]))
{
    $id     = $_POST["course_id"]; //get the courses code to remove
    $return_url = $_POST["return_url"]; //get return url

    foreach ($_SESSION["courses"] as $cart_itm) //loop through session array var
    {
        if($cart_itm["id"]==$id){ //item exist in the list

            //continue only if quantity is more than 1
            //removing item that has 0 qty
            if($cart_itm["qty"]>=1 && $_POST['qty']>=1) 
            {
            $qty = $_POST['qty']; //just change the quantity from 1 to qty post
            //prepare array for the coursess session
            $courses[] = array('title'=>$cart_itm["title"], 'id'=>$cart_itm["id"], 'qty'=>$qty, 'fee'=>$cart_itm["fee"]);
            }
            if($cart_itm["qty"]>=1 && $_POST['qty']=0) 
            {
            $qty = $qty = $cart_itm["qty"]*0; //just change the quantity from 1 to qty post
            //prepare array for the coursess session
            //$courses[] = array('title'=>$cart_itm["title"], 'id'=>$cart_itm["id"], 'qty'=>$qty, 'fee'=>$cart_itm["fee"]);
            }
        }else{
            $courses[] = array('title'=>$cart_itm["title"], 'id'=>$cart_itm["id"], 'qty'=>$cart_itm["qty"], 'fee'=>$cart_itm["fee"]);
        }

        //set session with new array values
        $_SESSION["courses"] = $courses;
    }

    //redirect back to original page
    header('Location:'.$return_url);
}
?>
Faisal
  • 1,731
  • 19
  • 28
  • 1
    possible duplicate of [Diagnosing Memory Leaks - Allowed memory size of # bytes exhausted](http://stackoverflow.com/questions/849549/diagnosing-memory-leaks-allowed-memory-size-of-bytes-exhausted) – Mike B Feb 10 '14 at 18:53
  • maybe you need revisit your code, – Ibu Feb 10 '14 at 18:53
  • 1
    Actually you have 256mb of available RAM. Post your code. – enapupe Feb 10 '14 at 18:54
  • I tried 256M too, but it isn't working – Faisal Feb 10 '14 at 18:56
  • Are both servers running the same php version? – malta Feb 10 '14 at 18:57
  • What are you actually trying to do that causes this error? You could always try posting some code for us to look at.. – The Blue Dog Feb 10 '14 at 19:09
  • @Micallef No. bluehost is running my website fine and running PHP Version 5.4.24 with memory_limit 128M where hostgator is running PHP Version 5.2.17 with memory_limit 256M and from hostgator I'm getting this error. I check all this using phpinfo() – Faisal Feb 10 '14 at 19:12
  • The PHP version is completely irrelevent. Again, what are you trying to do that causes this error? – The Blue Dog Feb 10 '14 at 19:13
  • I've added a shopping cart code from http://www.sanwebe.com/2013/06/creating-simple-shopping-cart-with-php after this I am faceing such issue – Faisal Feb 10 '14 at 19:18
  • ** No, what is YOUR code? Mate, if you don't post it then we can't help you. – The Blue Dog Feb 10 '14 at 19:21
  • @PeteR I've edited my question and put my code in it, hope you'll get it. – Faisal Feb 10 '14 at 23:23

2 Answers2

0

Contact hostgator support to verify this but in some cases they will either modify the php.ini for you or some hosting companies let you put a php.ini in an alternate location and you can set your options from there.

maskeda
  • 183
  • 1
  • 10
  • Yes, I contact customer support, although I'd access to php.ini and they also provide me default one but this couldn't fix my issue so they created a ticket on my behalf for Linux admin to investigate on this issue. – Faisal Feb 10 '14 at 23:22
0

This post is too old but it may useful to some one who have VPS with CentOS. I had this problem.

1.Open php.ini

CentOS:
$ nano /etc/php.ini

2.then find memory_limit, then change it to 512M as follow

CentOS:
File: /etc/php.ini
memory_limit = 512M;

In the case that you have multiPHP, you should change related php.ini to the version used by your code.

javad m
  • 169
  • 1
  • 6